flush
[m6w6/ext-psi] / src / parser_proc.y
index 62e8011992d36fa3d6acb530a0b1e0f3002f7fa2..0a7259ed8f3b082332bfc78328820bc5095fb92a 100644 (file)
 %extra_argument {PSI_Parser *P}
 /* TOKEN is defined inside syntax_error */
 %syntax_error {
-       PSI_ParserSyntaxError(P, P->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.
+%left PLUS MINUS 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->lib) {
-               PSI_ParserSyntaxError(P, P->fn, T->line, "Extra 'lib %s' statement has no effect", libname->text);
+       if (P->psi.file.ln) {
+               PSI_ParserSyntaxError(P, P->psi.file.ln, T->line, "Extra 'lib %s' statement has no effect", libname->text);
        } else {
-               P->lib = strndup(libname->text + 1, libname->size - 2);
+               P->psi.file.ln = strndup(libname->text + 1, libname->size - 2);
        }
        free(libname);
        free(T);
@@ -58,26 +64,25 @@ 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 const_type {const_type*}
-%destructor const_type {free_const_type($$);}
-const_type(type_) ::= BOOL(T). {
-       type_ = init_const_type(T->type, T->text);
-       free(T);
+%type struct_size {size_t}
+struct_size(size) ::= . {
+       size = 0;
 }
-const_type(type_) ::= INT(T). {
-       type_ = init_const_type(T->type, T->text);
-       free(T);
-}
-const_type(type_) ::= FLOAT(T). {
-       type_ = init_const_type(T->type, T->text);
-       free(T);
+struct_size(size) ::= COLON COLON LPAREN NUMBER(SIZ) RPAREN. {
+       size = atol(SIZ->text);
+       free(SIZ);
 }
-const_type(type_) ::= STRING(T). {
+
+%token_class const_type_token BOOL INT FLOAT STRING.
+%type const_type {const_type*}
+%destructor const_type {free_const_type($$);}
+const_type(type_) ::= const_type_token(T). {
        type_ = init_const_type(T->type, T->text);
        free(T);
 }
@@ -94,6 +99,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);
@@ -108,10 +119,25 @@ decl_typedef(def) ::= TYPEDEF decl_struct(s) NAME(ALIAS) EOS. {
 
 %type decl {decl*}
 %destructor decl {free_decl($$);}
-decl(decl) ::= decl_abi(abi) decl_arg(func) LPAREN decl_args(args) RPAREN EOS. {
+decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
        decl = init_decl(abi, func, args);
 }
 
+%type decl_func {decl_arg*}
+%destructor decl_func {free_decl_arg($$);}
+decl_func(func) ::= decl_arg(arg). {
+       func = arg;
+}
+/* special case for void functions */
+decl_func(func) ::= VOID(T) NAME(N). {
+       func = init_decl_arg(
+               init_decl_type(T->type, T->text),
+               init_decl_var(N->text, 0, 0)
+       );
+       free(T);
+       free(N);
+}
+
 %type decl_abi {decl_abi*}
 %destructor decl_abi {free_decl_abi($$);}
 decl_abi(abi) ::= NAME(T). {
@@ -121,20 +147,11 @@ decl_abi(abi) ::= NAME(T). {
 
 %type decl_var {decl_var*}
 %destructor decl_var {free_decl_var($$);}
-decl_var(var) ::= NAME(T). {
-       var = init_decl_var(T->text, 0, 0);
-       free(T);
-}
-decl_var(var) ::= pointers(p) NAME(T). {
+decl_var(var) ::= indirection(p) NAME(T). {
        var = init_decl_var(T->text, p, 0);
        free(T);
 }
-decl_var(var) ::= NAME(T) LBRACKET NUMBER(D) RBRACKET. {
-       var = init_decl_var(T->text, 1, atol(D->text));
-       free(T);
-       free(D);
-}
-decl_var(var) ::= pointers(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
+decl_var(var) ::= indirection(p) NAME(T) LBRACKET NUMBER(D) RBRACKET. {
        var = init_decl_var(T->text, p+1, atol(D->text));
        free(T);
        free(D);
@@ -151,12 +168,30 @@ 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). {
+       arg_ = init_decl_arg(
+               init_decl_type(T->type, T->text),
+               init_decl_var(N->text, p, 0)
+       );
+       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);
 }
 
 %type decl_args {decl_args*}
 %destructor decl_args {free_decl_args($$);}
+decl_args ::= .
 decl_args ::= VOID.
 decl_args(args) ::= decl_arg(arg). {
        args = init_decl_args(arg);
@@ -166,89 +201,67 @@ 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*}
 %destructor decl_type {free_decl_type($$);}
-decl_type(type_) ::= VOID(T). {
-       type_ = init_decl_type(T->type, T->text);
-       free(T);
-}
-decl_type(type_) ::= CHAR(T). {
+decl_type(type_) ::= decl_type_token(T). {
        type_ = init_decl_type(T->type, T->text);
        free(T);
 }
-decl_type(type_) ::= SHORT(T). {
+/* unsigned, urgh */
+decl_type(type_) ::= UNSIGNED NAME(T). {
        type_ = init_decl_type(T->type, T->text);
-       free(T);
+       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(T->type, T->text);
-       free(T);
-}
-decl_type(type_) ::= LONG(T). {
-       type_ = init_decl_type(T->type, T->text);
-       free(T);
-}
-decl_type(type_) ::= FLOAT(T). {
-       type_ = init_decl_type(T->type, T->text);
-       free(T);
-}
-decl_type(type_) ::= DOUBLE(T). {
-       type_ = init_decl_type(T->type, T->text);
-       free(T);
-}
-decl_type(type_) ::= SIZE_T(T). {
-       type_ = init_decl_type(T->type, T->text);
-       free(T);
-}
-decl_type(type_) ::= SINT8(T). {
-       type_ = init_decl_type(T->type, T->text);
-       free(T);
-}
-decl_type(type_) ::= UINT8(T). {
-       type_ = init_decl_type(T->type, T->text);
-       free(T);
-}
-decl_type(type_) ::= SINT16(T). {
-       type_ = init_decl_type(T->type, T->text);
-       free(T);
-}
-decl_type(type_) ::= UINT16(T). {
-       type_ = init_decl_type(T->type, T->text);
-       free(T);
-}
-decl_type(type_) ::= SINT32(T). {
-       type_ = init_decl_type(T->type, T->text);
-       free(T);
-}
-decl_type(type_) ::= UINT32(T). {
-       type_ = init_decl_type(T->type, T->text);
-       free(T);
-}
-decl_type(type_) ::= SINT64(T). {
-       type_ = init_decl_type(T->type, T->text);
-       free(T);
-}
-decl_type(type_) ::= UINT64(T). {
-       type_ = init_decl_type(T->type, T->text);
-       free(T);
-}
-decl_type(type_) ::= NAME(T). {
-       type_ = init_decl_type(T->type, T->text);
+       type_ = init_decl_type(PSI_T_NAME, T->text);
        free(T);
 }
+/* structs ! */
 decl_type(type_) ::= STRUCT(S) NAME(T). {
        type_ = init_decl_type(S->type, T->text);
        free(S);
        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. {
@@ -261,31 +274,16 @@ 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);
 }
 
+%token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
 %type impl_def_val {impl_def_val*}
 %destructor impl_def_val {free_impl_def_val($$);}
-impl_def_val(def) ::= NULL(T). {
-       def = init_impl_def_val(T);
-       free(T);
-}
-impl_def_val(def) ::= NUMBER(T). {
-       def = init_impl_def_val(T);
-       free(T);
-}
-impl_def_val(def) ::= TRUE(T). {
-       def = init_impl_def_val(T);
-       free(T);
-}
-impl_def_val(def) ::= FALSE(T). {
-       def = init_impl_def_val(T);
-       free(T);
-}
-impl_def_val(def) ::= QUOTED_STRING(T). {
-       def = init_impl_def_val(T);
+impl_def_val(def) ::= impl_def_val_token(T). {
+       def = init_impl_def_val(T->type, T->text);
        free(T);
 }
 
@@ -295,7 +293,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);
 }
@@ -361,53 +359,46 @@ 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);
-}
-let_value(val) ::= let_func(func) LPAREN impl_var(var) RPAREN. {
-       val = init_let_value(func, var, 0);
-}
-let_value(val) ::= REFERENCE let_func(func) LPAREN impl_var(var) RPAREN. {
-       val = init_let_value(func, var, 1);
-}
-let_value(val) ::= REFERENCE NULL. {
-       val = init_let_value(NULL, NULL, 1);
 }
-let_value(val) ::= NULL. {
-       val = init_let_value(NULL, NULL, 0);
+
+%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);
 }
 
-%type let_func {let_func*}
-%destructor let_func {free_let_func($$);}
-let_func(func) ::= STRLEN(T). {
-       func = init_let_func(T->type, T->text, NULL);
-       free(T);
+%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);
 }
-let_func(func) ::= STRVAL(T). {
-       func = init_let_func(T->type, T->text, NULL);
-       free(T);
+num_exp(exp) ::= decl_var(var). {
+       exp = init_num_exp(PSI_T_NAME, var);
 }
-let_func(func) ::= INTVAL(T). {
-       func = init_let_func(T->type, T->text, NULL);
-       free(T);
+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_func(func) ::= FLOATVAL(T). {
-       func = init_let_func(T->type, T->text, NULL);
-       free(T);
+
+let_value(val) ::= reference(r) let_func(func) LPAREN impl_var(var) RPAREN. {
+       val = init_let_value(func, var, r);
 }
-let_func(func) ::= BOOLVAL(T). {
-       func = init_let_func(T->type, T->text, NULL);
-       free(T);
+let_value(val) ::= reference(r) NULL. {
+       val = init_let_value(NULL, NULL, r);
 }
-let_func(func) ::= ARRVAL(T). {
+
+%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). {
        func = init_let_func(T->type, T->text, NULL);
        free(T);
 }
@@ -423,77 +414,71 @@ set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. {
 set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. {
        val = init_set_value(func, vars);
 }
-
-%type set_func {set_func*}
-%destructor set_func {free_set_func($$);}
-set_func(func) ::= TO_ARRAY(T). {
-       func = init_set_func(T->type, T->text);
-       free(T);
-}
-set_func(func) ::= TO_STRING(T). {
-       func = init_set_func(T->type, T->text);
-       free(T);
-}
-set_func(func) ::= TO_INT(T). {
-       func = init_set_func(T->type, T->text);
-       free(T);
+set_value(val) ::= set_func(func_) LPAREN decl_vars(vars_) COMMA set_vals(vals) RPAREN. {
+       val = vals;
+       val->func = func_;
+       val->vars = vars_;
 }
-set_func(func) ::= TO_FLOAT(T). {
-       func = init_set_func(T->type, T->text);
-       free(T);
+%type set_vals {set_value*}
+%destructor set_vals {free_set_value($$);}
+set_vals(vals) ::= set_value(val). {
+       vals = add_inner_set_value(init_set_value(NULL, NULL), val);
 }
-set_func(func) ::= TO_BOOL(T). {
-       func = init_set_func(T->type, T->text);
-       free(T);
+set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
+       vals = add_inner_set_value(vals_, val);
 }
-set_func(func) ::= VOID(T). {
+
+%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). {
        func = init_set_func(T->type, T->text);
        free(T);
 }
 
 %type return_stmt {return_stmt*}
 %destructor return_stmt {free_return_stmt($$);}
-return_stmt(ret) ::= RETURN set_func(func) LPAREN decl_var(var) RPAREN EOS. {
-       ret = init_return_stmt(func, var);
+return_stmt(ret) ::= RETURN set_value(val) EOS. {
+       ret = init_return_stmt(val);
 }
 
 %type free_stmt {free_stmt*}
 %destructor free_stmt {free_free_stmt($$);}
-free_stmt(free) ::= FREE decl_vars(vars) EOS. {
-       free = init_free_stmt(vars);
+free_stmt(free) ::= FREE free_calls(calls) EOS. {
+       free = init_free_stmt(calls);
 }
 
-%type impl_type {impl_type*}
-%destructor impl_type {free_impl_type($$);}
-impl_type(type_) ::= VOID(T). {
-       type_ = init_impl_type(T->type, T->text);
-       free(T);
+%type free_calls {free_calls*}
+%destructor free_calls {free_free_calls($$);}
+free_calls(calls) ::= free_call(call). {
+       calls = init_free_calls(call);
 }
-impl_type(type_) ::= MIXED(T). {
-       type_ = init_impl_type(T->type, T->text);
-       free(T);
-}
-impl_type(type_) ::= BOOL(T). {
-       type_ = init_impl_type(T->type, T->text);
-       free(T);
+free_calls(calls) ::= free_calls(calls_) COMMA free_call(call). {
+       calls = add_free_call(calls_, call);
 }
-impl_type(type_) ::= INT(T). {
-       type_ = init_impl_type(T->type, T->text);
-       free(T);
-}
-impl_type(type_) ::= FLOAT(T). {
-       type_ = init_impl_type(T->type, T->text);
-       free(T);
-}
-impl_type(type_) ::= STRING(T). {
-       type_ = init_impl_type(T->type, T->text);
-       free(T);
+
+%type free_call {free_call*}
+%destructor free_call {free_free_call($$);}
+free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
+       call = init_free_call(F->text, vars);
 }
-impl_type(type_) ::= ARRAY(T). {
+
+%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). {
        type_ = init_impl_type(T->type, T->text);
        free(T);
 }
 
+%type reference {char}
+reference(r) ::= . {r = 0;}
+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;}
-pointers(p) ::= pointers(P) POINTER. {p = P+1;}
+pointers(p) ::= ASTERISK. {p = 1;}
+pointers(p) ::= pointers(P) ASTERISK. {p = P+1;}