diff --git a/examples/test.psl b/examples/test.psl index 8988c9e..26ed9a7 100644 --- a/examples/test.psl +++ b/examples/test.psl @@ -1,10 +1,31 @@ +// Vectors + struct vec2: x : f32 y : f32 -func add(a : vec2, b : vec2) -> qwe: +func add(a : vec2, b : vec2) -> vec2: return vec2(a.x + b.x, a.y + b.y) mut v = add(vec2(1.0, 2.0), vec2(3.0, 4.0)) v.x = -v.x v.y = -v.y + +// Factorial + +func factorial(n : u32) -> u32: + if n == 0u: + return 1u + return n * factorial(n - 1u) + +let factorial10 = factorial(10u) + +// Fibonacci +func fib(n : u32) -> u32: + // Slow implementation with + // exponentially-growing recursion tree + if n == 0u | n == 1u: + return n // base case + return fib(n - 1u) + fib(n - 2u) + +let fib10 = fib(10u) diff --git a/libs/parser/rules/pslang.l b/libs/parser/rules/pslang.l index c9f9852..e7705c7 100644 --- a/libs/parser/rules/pslang.l +++ b/libs/parser/rules/pslang.l @@ -21,6 +21,8 @@ using bp = ::pslang::parser::bison::parser; [ ]+ { ctx.location.step(); } +"//"[^\n]* { return bp::make_comment(ctx.location); } + const { return bp::make_const(ctx.location); } let { return bp::make_let(ctx.location); } mut { return bp::make_mut(ctx.location); } diff --git a/libs/parser/rules/pslang.y b/libs/parser/rules/pslang.y index f6e30a1..782b734 100644 --- a/libs/parser/rules/pslang.y +++ b/libs/parser/rules/pslang.y @@ -61,6 +61,7 @@ template %token newline "newline" %token indent "indentation" +%token comment %token assignment "=" %token colon ":" %token comma "," @@ -169,8 +170,8 @@ module ; indented_statement_list -: indented_statement_list indentation statement newline { auto tmp = $1; tmp.statements.push_back({$2, std::make_unique($3)}); $$ = std::move(tmp); } -| indented_statement_list indentation newline { $$ = $1; } +: indented_statement_list indentation statement optional_comment newline { auto tmp = $1; tmp.statements.push_back({$2, std::make_unique($3)}); $$ = std::move(tmp); } +| indented_statement_list indentation optional_comment newline { $$ = $1; } | %empty { $$ = {}; } ; @@ -179,6 +180,11 @@ indentation | %empty { $$ = 0ull; } ; +optional_comment +: comment +| %empty +; + statement : expression { $$ = std::make_unique($1); } | expression assignment expression { $$ = ast::assignment{ std::make_unique($1), std::make_unique($3), @$ }; }