Small update

This commit is contained in:
2026-04-13 14:32:15 +02:00
parent 3758305a23
commit 2ef7c1c184
+58 -11
View File
@@ -1,10 +1,11 @@
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
COMMENT = _{ "//" ~ (!"\n" ~ ANY)* }
program = { SOI ~ (import_decl | function_decl | struct_decl | class_decl)* ~ EOI }
program = { SOI ~ item* ~ EOI }
item = _{ import_decl | function_decl | struct_decl | class_decl }
// primitives
identifier = @{ ASCII_ALPHA ~ (ASCII_ALPHANUMERIC | "_")* }
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
integer = @{ ASCII_DIGIT+ }
float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }
boolean = { "true" | "false" }
@@ -14,7 +15,9 @@ inner_str = @{ (!"\"" ~ ANY)* }
// top level
import_decl = { "import" ~ string_lit }
struct_decl = { "struct" ~ identifier ~ "{" ~ struct_field* ~ "}" }
class_decl = { "class" ~ identifier ~ "{" ~ (struct_field | function_decl)* ~ "}" }
class_decl = { "class" ~ identifier ~ "{" ~ class_member* ~ "}" }
class_member = _{ struct_field | function_decl }
function_decl = { "fn" ~ identifier ~ "(" ~ param_list? ~ ")" ~ ("->" ~ type_expr)? ~ block }
struct_field = { identifier ~ ":" ~ type_expr }
@@ -23,7 +26,14 @@ param = { identifier ~ ":" ~ type_expr }
// statements
block = { "{" ~ statement* ~ "}" }
statement = _{ let_stmt | return_stmt | if_stmt | for_stmt | assign_stmt | expression }
statement = _{
let_stmt
| return_stmt
| if_stmt
| for_stmt
| assign_stmt
| expression
}
assign_stmt = { identifier ~ "=" ~ expression }
let_stmt = { "let" ~ identifier ~ (":" ~ type_expr)? ~ "=" ~ expression }
@@ -31,14 +41,29 @@ return_stmt = { "return" ~ expression? }
if_stmt = { "if" ~ expression ~ block ~ ("else" ~ block)? }
for_stmt = { "for" ~ identifier ~ "in" ~ expression ~ block }
// types — optional is a suffix, not a recursive wrapper
// types
type_expr = { base_type ~ "?"? }
base_type = _{ array_type | identifier }
array_type = { "[" ~ type_expr ~ "]" }
// expressions
// expressions (still simple, no precedence yet)
expression = { term ~ (bin_op ~ term)* }
bin_op = _{ add | sub | mul | div | eq | neq | lte | gte | lt | gt | and | or }
bin_op = _{
eq
| neq
| lte
| gte
| lt
| gt
| and
| or
| add
| sub
| mul
| div
}
add = { "+" }
sub = { "-" }
mul = { "*" }
@@ -52,8 +77,13 @@ gt = { ">" }
and = { "&&" }
or = { "||" }
// literals
struct_literal = {
identifier ~ "{" ~ (struct_init_field ~ ("," ~ struct_init_field)*)? ~ "}"
identifier ~ "{" ~ struct_init_list? ~ "}"
}
struct_init_list = {
struct_init_field ~ ("," ~ struct_init_field)* ~ ","?
}
struct_init_field = {
@@ -61,12 +91,29 @@ struct_init_field = {
}
array_literal = {
"[" ~ (expression ~ ("," ~ expression)*)? ~ "]"
"[" ~ (expression ~ ("," ~ expression)*)? ~ ","? ~ "]"
}
// expressions continuation
term = { primary ~ (field_access | call_suffix)* }
field_access = { "." ~ identifier }
call_suffix = { "(" ~ (expression ~ ("," ~ expression)*)? ~ ")" }
primary = _{ struct_literal | array_literal | float | integer | string_lit | boolean | self_kw | null_kw | identifier }
call_suffix = {
"(" ~ (expression ~ ("," ~ expression)*)? ~ ","? ~ ")"
}
primary = _{
struct_literal
| array_literal
| float
| integer
| string_lit
| boolean
| self_kw
| null_kw
| identifier
}
self_kw = { "self" }
null_kw = { "null" }