X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Fparser_proc.y;h=c90062a365e1b1cd2b18a105337b87d797668dc2;hp=69d69555884964e6edecfe7d17649c889efb3b12;hb=514614a6765283ca9c74a48b0f6c6fd24d6fd8e2;hpb=596f215b67dd267284df4fe0e11f0ca4d197bfc1 diff --git a/src/parser_proc.y b/src/parser_proc.y index 69d6955..c90062a 100644 --- a/src/parser_proc.y +++ b/src/parser_proc.y @@ -15,18 +15,24 @@ %extra_argument {PSI_Parser *P} /* TOKEN is defined inside syntax_error */ %syntax_error { - PSI_ParserSyntaxError(P, P->psi.file.fn, P->line, "Unexpected token '%s'", TOKEN->text); + if (TOKEN && TOKEN->type != PSI_T_EOF) { + PSI_ParserSyntaxError(P, P->psi.file.fn, P->line, "Unexpected token '%s'", TOKEN->text); + } else { + PSI_ParserSyntaxError(P, P->psi.file.fn, P->line, "Unexpected end of input"); + } } %nonassoc NAME. -%fallback NAME FREE SET LET RETURN LIB. +%left PLUS MINUS. +%left SLASH ASTERISK. +%fallback NAME FREE SET LET RETURN LIB INT UNSIGNED. file ::= blocks. blocks ::= block. blocks ::= blocks block. -block ::= COMMENT. +block ::= EOF. block ::= LIB(T) QUOTED_STRING(libname) EOS. { if (P->psi.file.ln) { @@ -59,11 +65,21 @@ block ::= decl_struct(strct). { %type decl_struct {decl_struct*} %destructor decl_struct {free_decl_struct($$);} -decl_struct(strct) ::= STRUCT NAME(N) LBRACE struct_args(args) RBRACE. { +decl_struct(strct) ::= STRUCT NAME(N) struct_size(size_) LBRACE struct_args(args) RBRACE. { strct = init_decl_struct(N->text, args); + strct->size = size_; free(N); } +%type struct_size {size_t} +struct_size(size) ::= . { + size = 0; +} +struct_size(size) ::= COLON COLON LPAREN NUMBER(SIZ) RPAREN. { + size = atol(SIZ->text); + free(SIZ); +} + %token_class const_type_token BOOL INT FLOAT STRING. %type const_type {const_type*} %destructor const_type {free_const_type($$);} @@ -84,6 +100,12 @@ decl_typedef(def) ::= TYPEDEF decl_type(type) NAME(ALIAS) EOS. { def = init_decl_typedef(ALIAS->text, type); free(ALIAS); } +/* support opaque types */ +decl_typedef(def) ::= TYPEDEF VOID(V) NAME(ALIAS) EOS. { + def = init_decl_typedef(ALIAS->text, init_decl_type(V->type, V->text)); + free(V); + free(ALIAS); +} decl_typedef(def) ::= TYPEDEF STRUCT(S) NAME(N) NAME(ALIAS) EOS. { def = init_decl_typedef(ALIAS->text, init_decl_type(S->type, N->text)); free(ALIAS); @@ -147,8 +169,8 @@ decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). { %type decl_arg {decl_arg*} %destructor decl_arg {free_decl_arg($$);} -decl_arg(arg_) ::= decl_type(type) decl_var(var). { - arg_ = var->arg = init_decl_arg(type, var); +decl_arg(arg_) ::= const_decl_type(type) decl_var(var). { + arg_ = init_decl_arg(type, var); } /* void pointers need a specific rule */ decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). { @@ -156,7 +178,14 @@ decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). { init_decl_type(T->type, T->text), init_decl_var(N->text, p, 0) ); - arg_->var->arg = arg_; + free(T); + free(N); +} +decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). { + arg_ = init_decl_arg( + init_decl_type(T->type, T->text), + init_decl_var(N->text, p, 0) + ); free(T); free(N); } @@ -173,12 +202,29 @@ decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). { } %type struct_args {decl_args*} %destructor struct_args {free_decl_args($$);} -struct_args(args) ::= decl_arg(arg) EOS. { +struct_args(args) ::= struct_arg(arg). { args = init_decl_args(arg); } -struct_args(args) ::= struct_args(args_) decl_arg(arg) EOS. { +struct_args(args) ::= struct_args(args_) struct_arg(arg). { args = add_decl_arg(args_, arg); } +%type struct_arg {decl_arg*} +%destructor struct_arg {free_decl_arg($$);} +struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. { + arg_->layout = layout_; + arg = arg_; +} + +%type struct_layout {decl_struct_layout*} +%destructor struct_layout {free_decl_struct_layout($$);} +struct_layout(layout) ::= . { + layout = NULL; +} +struct_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. { + layout = init_decl_struct_layout(atol(POS->text), atol(SIZ->text)); + free(POS); + free(SIZ); +} %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME. %type decl_type {decl_type*} @@ -187,6 +233,15 @@ decl_type(type_) ::= decl_type_token(T). { type_ = init_decl_type(T->type, T->text); free(T); } +/* unsigned, urgh */ +decl_type(type_) ::= UNSIGNED NAME(T). { + type_ = init_decl_type(T->type, T->text); + type_->name = realloc(type_->name, T->size + sizeof("unsigned")); + memmove(type_->name + sizeof("unsigned"), type_->name, T->size); + memcpy(type_->name, "unsigned", sizeof("unsigned")-1); + type_->name[sizeof("unsigned")] = ' '; + type_->name[T->size + sizeof("unsigned")] = 0; +} /* we have to support plain int here because we have it in our lexer rules */ decl_type(type_) ::= INT(T). { type_ = init_decl_type(PSI_T_NAME, T->text); @@ -199,6 +254,15 @@ decl_type(type_) ::= STRUCT(S) NAME(T). { free(T); } +%type const_decl_type {decl_type*} +%destructor const_decl_type {free_decl_type($$);} +const_decl_type(type) ::= decl_type(type_). { + type = type_; +} +const_decl_type(type) ::= CONST decl_type(type_). { + type = type_; +} + %type impl {impl*} %destructor impl {free_impl($$);} impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. { @@ -211,7 +275,7 @@ impl_func(func) ::= FUNCTION NSNAME(NAME) impl_args(args) COLON impl_type(type). func = init_impl_func(NAME->text, args, type, 0); free(NAME); } -impl_func(func) ::= FUNCTION REFERENCE NSNAME(NAME) impl_args(args) COLON impl_type(type). { +impl_func(func) ::= FUNCTION AMPERSAND NSNAME(NAME) impl_args(args) COLON impl_type(type). { func = init_impl_func(NAME->text, args, type, 1); free(NAME); } @@ -230,7 +294,7 @@ impl_var(var) ::= DOLLAR NAME(T). { var = init_impl_var(T->text, 0); free(T); } -impl_var(var) ::= REFERENCE DOLLAR NAME(T). { +impl_var(var) ::= AMPERSAND DOLLAR NAME(T). { var = init_impl_var(T->text, 1); free(T); } @@ -296,17 +360,35 @@ let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. { %type let_value {let_value*} %destructor let_value {free_let_value($$);} -let_value(val) ::= CALLOC(F) LPAREN NUMBER(N) COMMA decl_type(t) RPAREN. { - val = init_let_value( - init_let_func(F->type, F->text, - init_let_calloc( - atol(N->text), t - ) - ), NULL, 0 - ); +let_value(val) ::= CALLOC(F) LPAREN let_calloc(alloc) RPAREN. { + val = init_let_value(init_let_func(F->type, F->text, alloc), NULL, 0); free(F); - free(N); } + +%type let_calloc {let_calloc*} +%destructor let_calloc {free_let_calloc($$);} +let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). { + alloc = init_let_calloc(nmemb, size); +} + +%token_class num_exp_token NUMBER NSNAME. +%token_class num_exp_op_token PLUS MINUS ASTERISK SLASH. +%type num_exp {num_exp*} +%destructor num_exp {free_num_exp($$);} +num_exp(exp) ::= num_exp_token(tok). { + exp = init_num_exp(tok->type, tok->text); + free(tok); +} +num_exp(exp) ::= decl_var(var). { + exp = init_num_exp(PSI_T_NAME, var); +} +num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). { + exp_->operator = operator_->type; + exp_->operand = operand_; + exp = exp_; + free(operator_); +} + let_value(val) ::= reference(r) let_func(func) LPAREN impl_var(var) RPAREN. { val = init_let_value(func, var, r); } @@ -314,7 +396,7 @@ let_value(val) ::= reference(r) NULL. { val = init_let_value(NULL, NULL, r); } -%token_class let_func_token ARRVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL. +%token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL. %type let_func {let_func*} %destructor let_func {free_let_func($$);} let_func(func) ::= let_func_token(T). { @@ -330,13 +412,23 @@ set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. { %type set_value {set_value*} %destructor set_value {free_set_value($$);} -set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. { - val = init_set_value(func, vars); +set_value(val) ::= set_func(func) LPAREN decl_var(var) RPAREN. { + val = init_set_value(func, init_decl_vars(var)); +} +set_value(val) ::= set_func(func) LPAREN decl_var(var) COMMA num_exp(num_) RPAREN. { + val = init_set_value(func, init_decl_vars(var)); + val->num = num_; +} +set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA set_vals(vals) RPAREN. { + val = vals; + val->func = func_; + val->vars = init_decl_vars(var); } -set_value(val) ::= set_func(func_) LPAREN decl_vars(vars_) COMMA set_vals(vals) RPAREN. { +set_value(val) ::= set_func(func_) LPAREN decl_var(var) COMMA num_exp(num_) COMMA set_vals(vals) RPAREN. { val = vals; val->func = func_; - val->vars = vars_; + val->num = num_; + val->vars = init_decl_vars(var); } %type set_vals {set_value*} %destructor set_vals {free_set_value($$);} @@ -347,7 +439,7 @@ set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). { vals = add_inner_set_value(vals_, val); } -%token_class set_func_token TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID. +%token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID. %type set_func {set_func*} %destructor set_func {free_set_func($$);} set_func(func) ::= set_func_token(T). { @@ -382,7 +474,7 @@ free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. { call = init_free_call(F->text, vars); } -%token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY. +%token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT. %type impl_type {impl_type*} %destructor impl_type {free_impl_type($$);} impl_type(type_) ::= impl_type_token(T). { @@ -392,12 +484,12 @@ impl_type(type_) ::= impl_type_token(T). { %type reference {char} reference(r) ::= . {r = 0;} -reference(r) ::= REFERENCE. {r = 1;} +reference(r) ::= AMPERSAND. {r = 1;} %type indirection {unsigned} indirection(i) ::= . {i = 0;} indirection(i) ::= pointers(p). {i = p;} %type pointers {unsigned} -pointers(p) ::= POINTER. {p = 1;} -pointers(p) ::= pointers(P) POINTER. {p = P+1;} +pointers(p) ::= ASTERISK. {p = 1;} +pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}