aahf
This commit is contained in:
parent
8bfc8d13bf
commit
4492720a1c
5 changed files with 130 additions and 2 deletions
|
@ -12,7 +12,7 @@ enum StatementTypes {
|
|||
ST_Builtin = 2, // All special builtins (declarations, control flow...)
|
||||
ST_Const = 3, // Constant
|
||||
ST_Var = 4, // Variable
|
||||
ST_Main = 5, // Type of the root node
|
||||
ST_Block = 5, // Scope blocks
|
||||
ST__end
|
||||
};
|
||||
|
||||
|
|
21
src/parser.c
21
src/parser.c
|
@ -56,11 +56,27 @@ Statement *variable_get(char *name){
|
|||
return stat;
|
||||
}
|
||||
|
||||
Statement *make_block(Statement *first){
|
||||
Statement *stat = &stack->statements[stack->curr_statement++];
|
||||
stat->type = ST_Block;
|
||||
stat->children = first;
|
||||
stat->child_n = 1;
|
||||
return stat;
|
||||
}
|
||||
|
||||
Statement *add_block(Statement *block, Statement *add){
|
||||
assert(add == &block->children[block->child_n]);
|
||||
block->child_n++;
|
||||
return block;
|
||||
}
|
||||
|
||||
FnSig assign_sig = {.n_param = 1};
|
||||
|
||||
Statement *make_operation(i32 type, i32 extrainf, char *name, i32 nparam
|
||||
Statement *param, ...){
|
||||
Statement *stat = &stack->statements[stack->curr_statement++];
|
||||
stat->children = &stack->statements[stack->curr_statement];
|
||||
stat->child_n = 0;
|
||||
MapItem *ret = hashmap_get(&stack->symbol_map, name);
|
||||
if(type != BI_assign && ret->type != T_fn)
|
||||
return NULL;
|
||||
|
@ -76,9 +92,12 @@ Statement *make_operation(i32 type, i32 extrainf, char *name, i32 nparam
|
|||
|
||||
va_list prm;
|
||||
va_start(prm, param);
|
||||
if(nparam > fnsig->nparam)
|
||||
return NULL;
|
||||
for(int i = 0; i < nparam; i++){
|
||||
Statement *prm_n = va_arg(prm, Statement*);
|
||||
|
||||
stat->children[stat->child_n++] = prm_n;
|
||||
stack->curr_statement++;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,5 +16,10 @@ Statement *declare(i32 type, Tag vtype, char *name);
|
|||
|
||||
Statement *variable_get(char *name);
|
||||
|
||||
Statement *make_block(Statement *first);
|
||||
|
||||
Statement *add_block(Statement *block, Statement *add);
|
||||
|
||||
Statement *make_operation(i32 type, i32 extainf, char *str, i32 nparam
|
||||
Statement *param, ...);
|
||||
void set_entry_point(Statement *statement);
|
||||
|
|
35
src/parser.l
35
src/parser.l
|
@ -9,7 +9,42 @@
|
|||
|
||||
%%
|
||||
|
||||
[()] return *yytext;
|
||||
|
||||
[:][_a-zA-Z]+ {
|
||||
yyvalue.str = yytext+1;
|
||||
return TYPE_U;
|
||||
}
|
||||
|
||||
/* Statements with special syntax */
|
||||
"if" return IF;
|
||||
"else" return ELSE;
|
||||
"fn" return FN;
|
||||
"var" return VAR;
|
||||
|
||||
":vec" return VEC;
|
||||
|
||||
[-+/*%_a-zA-Z][-+/*%_a-zA-Z0-9]+ {
|
||||
yyvalue.str = yytext;
|
||||
return G_IDENT;
|
||||
}
|
||||
|
||||
[0-9]+ {
|
||||
yyvalue.st = make_iconst(atoi(yytext));
|
||||
return CST;
|
||||
}
|
||||
|
||||
[<=>!] return *yytext;
|
||||
|
||||
">=" return GTEQ;
|
||||
"<=" return SMEQ;
|
||||
|
||||
[ \t\n]+ return WHITESPACE;
|
||||
|
||||
. yyerror("Invalid character");
|
||||
|
||||
%%
|
||||
|
||||
int yywrap(){
|
||||
return 1;
|
||||
}
|
||||
|
|
69
src/parser.y
69
src/parser.y
|
@ -1,11 +1,80 @@
|
|||
%{
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "types.h"
|
||||
#include "code_defs.h"
|
||||
#include "parser.h"
|
||||
|
||||
void yyerror(char *s);
|
||||
%}
|
||||
|
||||
%union {
|
||||
char *str;
|
||||
Statement *st;
|
||||
}
|
||||
|
||||
%token WHITESPACE
|
||||
%token <str> G_IDENT TYPE_U
|
||||
%token VEC
|
||||
%token VAR
|
||||
%token IF
|
||||
%nonassoc IFX
|
||||
%nonassoc ELSE
|
||||
%token WHILE
|
||||
%token FN
|
||||
%token GTEQ
|
||||
%token SMEQ
|
||||
%token <st> CST
|
||||
|
||||
%type <st> stmt stmt_list
|
||||
|
||||
%%
|
||||
|
||||
program:
|
||||
function '.' { exit(0); }
|
||||
|
|
||||
;
|
||||
|
||||
function:
|
||||
function stmt { set_entry_point($2); }
|
||||
|
|
||||
;
|
||||
|
||||
/*TODO*/
|
||||
type:
|
||||
TYPE_U
|
||||
| VEC TYPE_U
|
||||
;
|
||||
|
||||
stmt:
|
||||
'(' G_IDENT ')'
|
||||
{$$ = make_operation(ST_Call, 0, $2)}
|
||||
| WHITESPACE G_IDENT
|
||||
{$$ = variable_get($2);}
|
||||
| WHITESPACE CST
|
||||
{$$ = $2;}
|
||||
| '(' VAR WHITESPACE G_IDENT ')'
|
||||
{$$ = declare(BI_var,{.is_array=0,.type=T_null},$4); )}
|
||||
| '(' VAR TYPE WHITESPACE G_IDENT ')'
|
||||
| '(' IF stmt stmt_list ')'
|
||||
{$$ = make_operation(BI_if, 0, 2, $3, $4);}
|
||||
| '(' IF stmt stmt_list ')' '(' ELSE stmt_list ')'
|
||||
| '(' WHILE stmt stmt_list ')'
|
||||
| '(' FN stmt stmt_list ')'
|
||||
| '(' GTEQ stmt stmt ')'
|
||||
| '(' SMEQ stmt stmt ')'
|
||||
| '(' '<' stmt stmt ')'
|
||||
| '(' '>' stmt stmt ')'
|
||||
| '(' '=' stmt stmt ')'
|
||||
| '(' '!' stmt ')'
|
||||
| '(' G_IDENT stmt_list ')'
|
||||
;
|
||||
|
||||
stmt_list:
|
||||
stmt
|
||||
{$$ = make_block($1);}
|
||||
| stmt_list stmt
|
||||
{$$ = add_block($1, $2);}
|
||||
;
|
||||
|
||||
%%
|
||||
|
|
Loading…
Add table
Reference in a new issue