From c87f5a8871edf6880b894a00b180c554ffd46d0a Mon Sep 17 00:00:00 2001
From: Kimplul <kimi.h.kuparinen@gmail.com>
Date: Sun, 23 Mar 2025 22:29:11 +0200
Subject: start sketching out type system

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

(limited to 'src/parser.y')

diff --git a/src/parser.y b/src/parser.y
index 68725a8..7b2c651 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -74,7 +74,10 @@
 %token ELSE "else"
 %token NIL "nil"
 %token OWN "own"
+%token PUB "pub"
 %token MUT "mut"
+%token CONTINUE "continue"
+%token IMPORT "import"
 %token ERROR "error"
 %token DOT "."
 %token SCOPE "::"
@@ -97,11 +100,16 @@
 %left "." "=>" "(" ")"
 %left "::"
 
-%nterm <node> top unit proc call closure var expr statement body
+%nterm <node> top unit proc proc_decl call closure var expr statement body
 %nterm <node> vars exprs statements closures err trailing_closure
 %nterm <node> opt_vars opt_exprs opt_statements opt_trailing_closure opt_err opt_error
 %nterm <node> rev_vars rev_exprs rev_closures rev_statements
-%nterm <node> let if nil own unop binop construct
+%nterm <node> let if nil own unop binop construct import
+
+%nterm <node> struct struct_cont trait
+%nterm <node> type_param type_params opt_type_params
+%nterm <node> behaviour behaviours opt_behaviours
+%nterm <node> member members opt_members
 
 %nterm <type> type rev_types types opt_types
 
@@ -203,7 +211,9 @@ proc
 	: ID "(" opt_vars ")" body {
 		$$ = gen_proc($[ID], $[opt_vars], NULL, $[body], src_loc(@$));
 	}
-	| ID "(" opt_vars ")" ";" {
+
+proc_decl
+	: ID "(" opt_vars ")" ";" {
 		$$ = gen_proc($[ID], $[opt_vars], NULL, NULL, src_loc(@$));
 	}
 
@@ -413,8 +423,80 @@ body
 		$$ = gen_block($2, $3, src_loc(@$));
 	}
 
+behaviour
+	: APPLY ";" { $$ = gen_trait_apply($1, src_loc(@$)); }
+	| proc_decl ";"
+	| proc
+
+behaviours
+	: behaviours behaviour { $$ = $1; $1->n = $2; }
+	| behaviour
+
+opt_behaviours
+	: behaviours
+	| { $$ = NULL; }
+
+member
+	: var ";"
+	| behaviour
+
+members
+	: members member { $$ = $1; $1->n = $2; }
+	| member
+
+opt_members
+	: members
+	| { $$ = NULL; }
+
+type_param
+	: ID ID {
+		struct type *t = tgen_id($1, src_loc(@1));
+		$$ = gen_var($2, t, src_loc(@$));
+	}
+
+type_params
+	: type_param "," type_params {
+		$$ = $1; $1->n = $3;
+	}
+	| type_param
+
+opt_type_params
+	: type_params
+	| { $$ = NULL; }
+
+struct
+	: ID "[" opt_type_params "]" "{" opt_members "}" {
+		$$ = gen_struct($1, $3, $6, src_loc(@$));
+	}
+
+struct_cont
+	: "continue" ID "[" opt_type_params "]" "{" opt_behaviours "}" {
+		$$ = gen_struct_cont($2, $4, $7, src_loc(@$));
+	}
+
+trait
+	: ID "{" opt_behaviours "}" {
+		$$ = gen_trait($1, $3, src_loc(@$));
+	}
+
+import
+	: "import" STRING {
+		$$ = gen_import($2, src_loc(@$));
+	}
+
 top
 	: proc
+	| proc_decl
+	| struct
+	| struct_cont
+	| import
+	| trait
+	| "pub" proc        { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); }
+	| "pub" proc_decl   { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); }
+	| "pub" struct      { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); }
+	| "pub" struct_cont { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); }
+	| "pub" import      { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); }
+	| "pub" trait       { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); }
 	| error {
 	    $$ = gen_empty(src_loc(@$));
 	    parser->failed = true;
-- 
cgit v1.2.3