flush+
[m6w6/ext-psi] / src / parser_proc.y
index d99c96d482bb956966915960d6508631b621b622..85fac69d8ac37e996d18edb06b7de72cee18db64 100644 (file)
 %token_prefix PSI_T_
 %token_type {PSI_Token *}
 %token_destructor {free($$);}
+%default_destructor {(void)P;}
 %extra_argument {PSI_Parser *P}
 /* TOKEN is defined inside syntax_error */
 %syntax_error {
-       PSI_ParserSyntaxError(P, P->fn, P->line, "Unexpected token '%s'.\n", TOKEN->text);
+       PSI_ParserSyntaxError(P, P->fn, P->line, "Unexpected token '%s'", TOKEN->text);
 }
 file ::= blocks.
 
@@ -25,7 +26,7 @@ block ::= COMMENT.
 
 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
        if (P->lib) {
-               PSI_ParserSyntaxError(P, P->fn, T->line, "Extra 'lib %s' statement has no effect.\n", libname->text);
+               PSI_ParserSyntaxError(P, P->fn, T->line, "Extra 'lib %s' statement has no effect", libname->text);
        } else {
                P->lib = strndup(libname->text + 1, libname->size - 2);
        }
@@ -42,6 +43,32 @@ block ::= impl(impl). {
 block ::= decl_typedef(def). {
        P->defs = add_decl_typedef(P->defs, def);
 }
+block ::= constant(constant). {
+       P->consts = add_constant(P->consts, constant);
+}
+
+%type const_type {const_type*}
+const_type(type_) ::= BOOL(T). {
+       type_ = init_const_type(T->type, T->text);
+       free(T);
+}
+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);
+}
+const_type(type_) ::= STRING(T). {
+       type_ = init_const_type(T->type, T->text);
+       free(T);
+}
+%type constant {constant*}
+constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
+       constant = init_constant(type, T->text, val);
+       free(T);
+}
 
 %type decl_typedef {decl_typedef*}
 decl_typedef(def) ::= TYPEDEF NAME(ALIAS) decl_type(type) EOS. {
@@ -57,6 +84,7 @@ decl(decl) ::= decl_abi(abi) decl_arg(func) LPAREN decl_args(args) RPAREN EOS. {
 %type decl_abi {decl_abi*}
 decl_abi(abi) ::= NAME(T). {
        abi = init_decl_abi(T->text);
+       free(T);
 }
 
 %type decl_var {decl_var*}
@@ -96,10 +124,22 @@ decl_type(type_) ::= VOID(T). {
        type_ = init_decl_type(T->type, T->text);
        free(T);
 }
+decl_type(type_) ::= CHAR(T). {
+       type_ = init_decl_type(T->type, T->text);
+       free(T);
+}
+decl_type(type_) ::= SHORT(T). {
+       type_ = init_decl_type(T->type, T->text);
+       free(T);
+}
 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);
@@ -146,35 +186,41 @@ decl_type(type_) ::= NAME(T). {
 }
 
 %type impl {impl*}
+%destructor impl {free_impl($$);}
 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
        impl = init_impl(func, stmts);
 }
 
 %type impl_func {impl_func*}
-impl_func(func) ::= FUNCTION NSNAME(NAME) LPAREN impl_args(args) RPAREN COLON impl_type(type). {
-       func = init_impl_func(NAME->text, args, type);
+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 NSNAME(NAME) LPAREN RPAREN COLON impl_type(type). {
-       func = init_impl_func(NAME->text, NULL, type);
+impl_func(func) ::= FUNCTION REFERENCE NSNAME(NAME) impl_args(args) COLON impl_type(type). {
+       func = init_impl_func(NAME->text, args, type, 1);
        free(NAME);
 }
 
 %type impl_def_val {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);
+       free(T);
 }
 
 %type impl_var {impl_var*}
@@ -196,14 +242,22 @@ impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
 }
 
 %type impl_args {impl_args*}
-impl_args(args) ::= impl_arg(arg). {
+impl_args(args) ::= LPAREN RPAREN. {
+       args = NULL;
+}
+impl_args(args) ::= LPAREN impl_arg_list(args_) RPAREN. {
+       args = args_;
+}
+%type impl_arg_list {impl_args*}
+impl_arg_list(args) ::= impl_arg(arg). {
        args = init_impl_args(arg);
 }
-impl_args(args) ::= impl_args(args_) COMMA impl_arg(arg). {
+impl_arg_list(args) ::= impl_arg_list(args_) COMMA impl_arg(arg). {
        args = add_impl_arg(args_, arg);
 }
 
 %type impl_stmts {impl_stmts*}
+%destructor impl_stmts {free_impl_stmts($$);}
 impl_stmts(stmts) ::= impl_stmt(stmt). {
        stmts = init_impl_stmts(stmt);
 }
@@ -212,14 +266,18 @@ impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
 }
 
 %type impl_stmt {impl_stmt*}
+%destructor impl_stmt {free_impl_stmt($$);}
 impl_stmt(stmt) ::= let_stmt(let). {
        stmt = init_impl_stmt(PSI_T_LET, let);
 }
 impl_stmt(stmt) ::= set_stmt(set). {
        stmt = init_impl_stmt(PSI_T_SET, set);
 }
-impl_stmt(stmt) ::= ret_stmt(ret). {
-       stmt = init_impl_stmt(PSI_T_RET, ret);
+impl_stmt(stmt) ::= return_stmt(ret). {
+       stmt = init_impl_stmt(PSI_T_RETURN, ret);
+}
+impl_stmt(stmt) ::= free_stmt(free). {
+       stmt = init_impl_stmt(PSI_T_FREE, free);
 }
 
 %type let_stmt {let_stmt*}
@@ -231,6 +289,9 @@ let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. {
 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);
 }
@@ -292,9 +353,14 @@ set_func(func) ::= VOID(T). {
        free(T);
 }
 
-%type ret_stmt {ret_stmt*}
-ret_stmt(ret) ::= RET set_func(func) LPAREN decl_var(var) RPAREN EOS. {
-       ret = init_ret_stmt(func, var);
+%type return_stmt {return_stmt*}
+return_stmt(ret) ::= RETURN set_func(func) LPAREN decl_var(var) RPAREN EOS. {
+       ret = init_return_stmt(func, var);
+}
+
+%type free_stmt {free_stmt*}
+free_stmt(free) ::= FREE decl_vars(vars) EOS. {
+       free = init_free_stmt(vars);
 }
 
 %type impl_type {impl_type*}
@@ -329,4 +395,4 @@ impl_type(type_) ::= ARRAY(T). {
 
 %type pointers {unsigned}
 pointers(p) ::= POINTER. {++p;}
-pointers(p) ::= pointers(P) POINTER. {p = ++P;}
+pointers(p) ::= pointers(P) POINTER. {p = P+1;}