lljkjkjkj
This commit is contained in:
parent
883f7dad9c
commit
71bc6fd203
3 changed files with 83 additions and 15 deletions
2
spec.md
2
spec.md
|
@ -80,7 +80,7 @@ In this document :
|
|||
of type type, and optional size size (otherwise size 0)
|
||||
(if type isn't specified it is inferred)
|
||||
|
||||
- `(let[:type] <name>)`:
|
||||
- `(let[:type] <name> [size])`:
|
||||
Same as var, but variable is of local scope (recommended)
|
||||
|
||||
- `(if <cond> [expressions])`:
|
||||
|
|
|
@ -14,32 +14,65 @@ enum StatementTypes {
|
|||
ST_Var = 4 // Variable
|
||||
};
|
||||
|
||||
// Special expressions and ones that map directly to bytecode
|
||||
enum BuiltinStatements {
|
||||
|
||||
BI_assign = 0,
|
||||
BI_var = 1,
|
||||
BI_let = 2,
|
||||
BI_if = 3,
|
||||
BI_else = 4,
|
||||
BI_while = 5,
|
||||
BI_fn = 6,
|
||||
BI_import = 7,
|
||||
BI_add = 8,
|
||||
BI_sub = 9,
|
||||
BI_mul = 10,
|
||||
BI_div = 11,
|
||||
BI_mod = 12,
|
||||
BI_sml = 13,
|
||||
BI_sml_eq = 14,
|
||||
BI_eq = 15,
|
||||
BI_gt_eq = 16,
|
||||
BI_gt = 17,
|
||||
BI_not = 18,
|
||||
BI_and = 19,
|
||||
BI_or = 20,
|
||||
BI_is = 21,
|
||||
BI_cast = 22,
|
||||
BI_len = 23,
|
||||
BI_push = 24,
|
||||
BI_pop = 25
|
||||
};
|
||||
|
||||
struct Statement;
|
||||
|
||||
typedef struct Statement {
|
||||
struct Statement {
|
||||
|
||||
i32 type;
|
||||
i32 is_const; // Statement is constant, != is a constant
|
||||
struct Statement *children;
|
||||
i32 child_n;
|
||||
enum {
|
||||
union {
|
||||
Value cons;
|
||||
i32 fn_id;
|
||||
i32 builtin_type;
|
||||
Value cons;
|
||||
}
|
||||
i32 var_id;
|
||||
};
|
||||
};
|
||||
|
||||
} Statement;
|
||||
typedef struct Statement Statement;
|
||||
|
||||
// In practice, this is allocated in parse.c
|
||||
typedef struct {
|
||||
|
||||
// Kept for debug messages
|
||||
i32 curr_line;
|
||||
i32 curr_column;
|
||||
|
||||
i32 stack_size;
|
||||
i32 max_statment;
|
||||
i32 curr_statement;
|
||||
HashMap *symbol_map;
|
||||
HashMap symbol_map;
|
||||
char **pos_stack; // Pointers to text pos of statements, freed after parsing
|
||||
Statement statements[];
|
||||
|
||||
|
|
49
src/parser.c
49
src/parser.c
|
@ -13,13 +13,13 @@ ASTStack *make_stack(){
|
|||
ret->pos_stack = malloc(sizeof(void*)*2048);
|
||||
if(!ret->pos_stack)
|
||||
return NULL;
|
||||
ret->symbol_map = heap_hashmap(SYMBOL_MAP_S);
|
||||
if(heap_hashmap(&ret->symbol_map, SYMBOL_MAP_S))
|
||||
return NULL;
|
||||
ret->stack_size = 2048;
|
||||
return ret;
|
||||
}
|
||||
|
||||
i32 parenth_stack[256];
|
||||
i32 parenth_d;
|
||||
i32 global_parenth_d;
|
||||
|
||||
// Does the final transformation -> recursion end
|
||||
int parse_statement_final(ASTStack *stack){
|
||||
|
@ -29,18 +29,54 @@ int parse_statement_final(ASTStack *stack){
|
|||
// Recursively extracts statements
|
||||
int parse_statement(ASTStack *stack){
|
||||
i32 curr = stack->curr_statement;
|
||||
Statement *stat = &stack->statements[curr];
|
||||
stat->children = &stack->statements[stack->max_statement];
|
||||
stat->child_n = 0;
|
||||
i32 parent_d = 0;
|
||||
char **curr_pos = &stack->pos_stack[curr];
|
||||
char ch = ' ';
|
||||
do {
|
||||
i32 statement_complete;
|
||||
|
||||
i32 ret = 0;
|
||||
do {
|
||||
switch(ch){
|
||||
case '(':
|
||||
parenth_stack[parenth_d++] = 1;
|
||||
case '\n':
|
||||
stack->curr_line++;
|
||||
stack->curr_column = -1;
|
||||
break;
|
||||
|
||||
case ' ':
|
||||
break;
|
||||
|
||||
case '(':
|
||||
parenth_d++;
|
||||
// It's another statment than our main one
|
||||
if(parenth_d > 1){
|
||||
stack->max_statement++;
|
||||
stat->child_n++;
|
||||
}
|
||||
break;
|
||||
|
||||
case ')':
|
||||
if(parenth_d)
|
||||
parenth_d--;
|
||||
else // We have reached the end of our statement
|
||||
goto ps_exit;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
ch = **curr_pos;
|
||||
*curr_pos++;
|
||||
stack->curr_column++;
|
||||
} while(ch);
|
||||
|
||||
ps_exit:
|
||||
|
||||
stack->curr_statement++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ASTStack *parse(char *text){
|
||||
|
@ -52,7 +88,6 @@ ASTStack *parse(char *text){
|
|||
|
||||
i32 ret = 0;
|
||||
stack->pos_stack[0] = text;
|
||||
stack->statements[0] = NULL;
|
||||
do {
|
||||
ret = parse_statement(stack);
|
||||
} while(!ret);
|
||||
|
|
Loading…
Add table
Reference in a new issue