From: Brendan Hansen Date: Tue, 1 Nov 2022 18:55:34 +0000 (-0500) Subject: added array subscript X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=fd903d33a631348ff0f780f001f2c14aa1389067;p=onyxlang.io.git added array subscript --- diff --git a/src/html-templates/src/parser.onyx b/src/html-templates/src/parser.onyx index c48935f..0f5cf26 100644 --- a/src/html-templates/src/parser.onyx +++ b/src/html-templates/src/parser.onyx @@ -1,7 +1,7 @@ package otmp -use core {string, array, iter} +use core {string, array, iter, conv} use core.alloc {as_allocator} ParseError :: enum { @@ -49,10 +49,14 @@ TemplateToken :: struct { Keyword_Extends; String_Literal; + Int_Literal; Variable; Symbol; + Dot; + Open_Bracket; + Close_Bracket; } type: Type; @@ -170,6 +174,8 @@ TemplateToken :: struct { token_consume("in", .Keyword_In); token_consume("extends", .Keyword_Extends); token_consume(".", .Dot); + token_consume("[", .Open_Bracket); + token_consume("]", .Close_Bracket); if self.s.data[0] == #char "\"" { // :TODO add escaped strings @@ -195,6 +201,17 @@ TemplateToken :: struct { yield_token(.Variable); } + if self.s.data[0]->is_num() { + chars := 0; + while chars < self.s.length && self.s.data[chars]->is_num() { + chars += 1; + } + + tkn.text = self->eat_characters(chars); + + yield_token(.Int_Literal); + } + if self.s.data[0]->is_alphanum() { chars := 0; while chars < self.s.length && @@ -403,6 +420,15 @@ parse_expression :: (use p: ^TemplateParser) -> (^TExpr, ParseError) { retval = var_expr; } + + case .Int_Literal { + value: i32 = ~~ conv.str_to_i64(tkn.text); + + int_expr := make_expr(t, TExprInt); + int_expr.val = value; + + retval = int_expr; + } } if retval == null { @@ -413,17 +439,13 @@ parse_expression :: (use p: ^TemplateParser) -> (^TExpr, ParseError) { case .Dot { p.l->consume(); - sym_tkn: TemplateToken; - - // this is gross... - if err := do { - expect_token(p, .Symbol, #(sym_tkn)); - return .None; - }; err != .None { + if (p.l->peek()).type != .Symbol { err = .Unexpected_Token; break break; } + sym_tkn := p.l->consume(); + select_expr := make_expr(t, TExprSelector); select_expr.var = retval; select_expr.field = sym_tkn.text |> string.alloc_copy(as_allocator(^t.node_storage)); @@ -431,6 +453,26 @@ parse_expression :: (use p: ^TemplateParser) -> (^TExpr, ParseError) { retval = select_expr; } + case .Open_Bracket { + p.l->consume(); + + expr, err' := parse_expression(p); + if err != .None do break break; + + subscript_expr := make_expr(t, TExprSubscript); + subscript_expr.var = retval; + subscript_expr.sub = expr; + + retval = subscript_expr; + + if (p.l->peek()).type != .Close_Bracket { + err = .Unexpected_Token; + break break; + } + + p.l->consume(); + } + case #default do break break; } diff --git a/src/html-templates/src/render.onyx b/src/html-templates/src/render.onyx index a24a095..7610f03 100644 --- a/src/html-templates/src/render.onyx +++ b/src/html-templates/src/render.onyx @@ -1,7 +1,12 @@ package otmp use core {io, tprintf} -use core.misc {any_iter, any_dereference, any_selector} +use core.misc { + any_iter, + any_dereference, + any_selector, + any_subscript +} #package TemplateRenderer :: struct { @@ -86,7 +91,7 @@ render_instructions :: (use r: ^TemplateRenderer, instrs: [..] ^TNode) -> Error } } - case TExprVar, TExprSelector { + case TExprVar, TExprSelector, TExprSubscript { var := resolve_expr_to_any(r, cast(^TExpr) it); if !var.data do continue; @@ -119,7 +124,36 @@ resolve_expr_to_any :: (use r: ^TemplateRenderer, expr: ^TExpr) -> any { sub_any = any_dereference(sub_any); return any_selector(sub_any, selector.field); } + + case TExprSubscript { + subscript := cast(^TExprSubscript) expr; + + sub_any := resolve_expr_to_any(r, subscript.var); + sub := any_to_int(resolve_expr_to_any(r, subscript.sub)); + + core.printf("{} {}\n", sub_any, sub); + + return any_subscript(sub_any, ~~ sub); + } } return .{null, void}; } + + +#local +// :StandardLibrary +any_to_int :: (v: any) -> i64 { + switch v.type { + C(u32); + C(i32); + C(u64); + C(i64); + + C :: macro (T: type_expr) { + case T do return ~~ *cast(^T) v.data; + } + } + + return 0; +} diff --git a/src/html-templates/src/types.onyx b/src/html-templates/src/types.onyx index 90286bb..a97d166 100644 --- a/src/html-templates/src/types.onyx +++ b/src/html-templates/src/types.onyx @@ -73,6 +73,19 @@ TExprSelector :: struct { field: str; // storage: template.node_storage } +TExprSubscript :: struct { + use expr: TExpr; + + var: ^TExpr; + sub: ^TExpr; +} + +TExprInt :: struct { + use expr: TExpr; + + val: i32; +} + Error :: enum { None; diff --git a/www/templates/index.html b/www/templates/index.html index 484b9c5..3ed620f 100644 --- a/www/templates/index.html +++ b/www/templates/index.html @@ -31,7 +31,7 @@ -

Name: {% $test.templates %}

+

Name: {% $test.templates.entries[0] %}

Age: {% $test.arena %}

{{endblock}}