This commit is contained in:
Steve Biedermann 2026-04-29 18:35:55 +02:00
parent fd00f725f5
commit e1be190beb
4 changed files with 26 additions and 5 deletions

View File

@ -12,7 +12,7 @@ int sum_array(int[] arr) {
} }
int main() { int main() {
struct Point p; Point p;
int[] nums; int[] nums;
int x = 1 + 2 * 3; int x = 1 + 2 * 3;
p.x = x; p.x = x;

View File

@ -2,7 +2,7 @@ import "modules/types.spooky";
import "modules/math.spooky"; import "modules/math.spooky";
int main() { int main() {
struct Point p; Point p;
p.x = 1; p.x = 1;
p.y = 2; p.y = 2;
return add(p.x, p.y); return add(p.x, p.y);

View File

@ -10,6 +10,9 @@ type parser_state = {
let mk_state toks = { toks = Array.of_list toks; i = 0 } let mk_state toks = { toks = Array.of_list toks; i = 0 }
let peek st = if st.i < Array.length st.toks then st.toks.(st.i) else TEOF let peek st = if st.i < Array.length st.toks then st.toks.(st.i) else TEOF
let peek_n st n =
let idx = st.i + n in
if idx < Array.length st.toks then st.toks.(idx) else TEOF
let consume st = let consume st =
let t = peek st in let t = peek st in
@ -61,7 +64,23 @@ let expect_ident st =
| TIdent s -> s | TIdent s -> s
| _ -> raise (Parse_error "expected identifier") | _ -> raise (Parse_error "expected identifier")
let starts_type = function TIntKw | TBoolKw | TVoidKw | TStructKw -> true | _ -> false let starts_builtin_type = function TIntKw | TBoolKw | TVoidKw | TStructKw -> true | _ -> false
let rec skip_array_suffixes st j =
match peek_n st j with
| TLBracket ->
(match peek_n st (j + 1) with
| TRBracket -> skip_array_suffixes st (j + 2)
| _ -> j)
| _ -> j
let looks_like_type_start st =
match peek st with
| t when starts_builtin_type t -> true
| TIdent _ ->
let j = skip_array_suffixes st 1 in
(match peek_n st j with TIdent _ -> true | _ -> false)
| _ -> false
let rec parse_type st = let rec parse_type st =
let base = let base =
@ -70,6 +89,7 @@ let rec parse_type st =
| TBoolKw -> TBool | TBoolKw -> TBool
| TVoidKw -> TVoid | TVoidKw -> TVoid
| TStructKw -> TStruct (expect_ident st) | TStructKw -> TStruct (expect_ident st)
| TIdent s -> TStruct s
| _ -> raise (Parse_error "expected type") | _ -> raise (Parse_error "expected type")
in in
let rec arrays t = let rec arrays t =
@ -115,6 +135,7 @@ and parse_top st =
let ty = TStruct sname in let ty = TStruct sname in
parse_top_after_type st ty) parse_top_after_type st ty)
| _ -> | _ ->
if not (looks_like_type_start st) then raise (Parse_error "expected top-level declaration");
let ty = parse_type st in let ty = parse_type st in
parse_top_after_type st ty parse_top_after_type st ty
@ -208,7 +229,7 @@ and parse_stmt st =
in in
expect st TSemicolon; expect st TSemicolon;
Return v Return v
| t when starts_type t -> | _ when looks_like_type_start st ->
let ty = parse_type st in let ty = parse_type st in
let n = expect_ident st in let n = expect_ident st in
let init = let init =

View File

@ -14,7 +14,7 @@ int fold(int[] xs) {
int main() { int main() {
int[] xs; int[] xs;
struct Item it; Item it;
int y = 2 + 3 * 4; int y = 2 + 3 * 4;
it.value = y; it.value = y;
if (y >= 0) { if (y >= 0) {