From 8ee9c16bfb4090cab4a330915a373047d82bc459 Mon Sep 17 00:00:00 2001
From: Kimplul <kimi.h.kuparinen@gmail.com>
Date: Sun, 25 Feb 2024 21:50:50 +0200
Subject: initial parser

---
 src/parser.y | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 139 insertions(+), 3 deletions(-)

(limited to 'src/parser.y')

diff --git a/src/parser.y b/src/parser.y
index 8a4ddb5..ee1a026 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -141,10 +141,145 @@ static void yyerror(YYLTYPE *yylloc, void *lexer,
 
 %}
 
-%start input;
+%start program;
 %%
 
-input: /* empty */
+statement_list
+	: statement "," statement_list
+	| statement
+
+definitions
+	: function_definition
+	| procedure_definition
+	| variable_definition
+
+variable_definition
+	: "var" IDENT "=" expression
+
+variable_definitions
+	: variable_definition variable_definitions
+	| variable_definition
+
+opt_variable_definitions
+	: variable_definitions
+	|
+
+return
+	: "return" IDENT
+
+opt_return
+	: return
+	|
+
+function_definition
+	: "function" FUNC_IDENT "{" opt_formals "}"
+	return opt_variable_definitions "is" rvalue "end" "function"
+
+procedure_definition
+	: "procedure" PROC_IDENT "(" opt_formals "}"
+	opt_return opt_variable_definitions "is" statement_list
+	"end" "procedure"
+
+formals
+	: formal_arg "," formal_arg
+	| formal_arg
+
+opt_formals
+	: formals
+	|
+
+formal_arg
+	: IDENT "[" IDENT "]"
+
+procedure_call
+	: PROC_IDENT "(" opt_arguments ")"
+
+arguments
+	: expression "," expression
+	| expression
+
+opt_arguments
+	: arguments
+	|
+
+assignment
+	: lvalue "=" rvalue
+
+lvalue
+	: IDENT
+	| IDENT "." IDENT
+
+rvalue
+	: expression
+	| unless_expression
+
+print_statement
+	: "print" print_items
+
+print_items
+	: print_item "&" print_items
+	| print_item
+
+print_item
+	: STRING
+	| expression
+
+statement
+	: procedure_call
+	| assignment
+	| print_statement
+	| until_statement
+	| unless_statement
+	| "return" expression
+
+until_statement
+	: "do" statement_list "until" expression
+
+unless_statement
+	: "do" statement_list "unless" expression
+	| "do" statement_list "unless" expression "otherwise" statement_list "done"
+
+expression
+	: simple_expr
+	| expression "=" simple_expr
+	| expression "<" simple_expr
+
+simple_expr
+	: term
+	| simple_expr "+" term
+	| simple_expr "-" term
+
+term
+	: factor
+	| term "*" factor
+	| term "/" factor
+
+factor
+	: "+" atom
+	| "-" atom
+	| atom
+
+atom
+	: IDENT
+	| IDENT "'" IDENT
+	| INT_LITERAL
+	| DATE_LITERAL
+	| function_call
+	| procedure_call
+	| "(" expression ")"
+
+function_call
+	: FUNC_IDENT "(" opt_arguments ")"
+
+unless_expression
+	: "do" expression "unless" expression "otherwise" expression "done"
+
+opt_definitions
+	: definitions
+	| {}
+
+program
+	: opt_definitions statement_list
 
 %%
 
@@ -239,5 +374,6 @@ void parse(struct parser *p, const char *fname, const char *buf)
 	// runs
 	dump_lex(p);
 
-	// yyparse(p->lexer, p);
+	yy_scan_string(buf, p->lexer);
+	yyparse(p->lexer, p);
 }
-- 
cgit v1.2.3