Add single-line comments

This commit is contained in:
Nikita Lisitsa 2025-12-21 15:05:55 +03:00
parent 5419f87a56
commit 049cf7335a
3 changed files with 32 additions and 3 deletions

View file

@ -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)

View file

@ -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); }

View file

@ -61,6 +61,7 @@ template <typename T>
%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<ast::statement>($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<ast::statement>($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<ast::expression>($1); }
| expression assignment expression { $$ = ast::assignment{ std::make_unique<ast::expression>($1), std::make_unique<ast::expression>($3), @$ }; }