Small update
This commit is contained in:
+77
-30
@@ -1,10 +1,11 @@
|
|||||||
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
|
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
|
||||||
COMMENT = _{ "//" ~ (!"\n" ~ ANY)* }
|
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
|
// primitives
|
||||||
identifier = @{ ASCII_ALPHA ~ (ASCII_ALPHANUMERIC | "_")* }
|
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
|
||||||
integer = @{ ASCII_DIGIT+ }
|
integer = @{ ASCII_DIGIT+ }
|
||||||
float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }
|
float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }
|
||||||
boolean = { "true" | "false" }
|
boolean = { "true" | "false" }
|
||||||
@@ -12,9 +13,11 @@ string_lit = { "\"" ~ inner_str ~ "\"" }
|
|||||||
inner_str = @{ (!"\"" ~ ANY)* }
|
inner_str = @{ (!"\"" ~ ANY)* }
|
||||||
|
|
||||||
// top level
|
// top level
|
||||||
import_decl = { "import" ~ string_lit }
|
import_decl = { "import" ~ string_lit }
|
||||||
struct_decl = { "struct" ~ identifier ~ "{" ~ struct_field* ~ "}" }
|
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 }
|
function_decl = { "fn" ~ identifier ~ "(" ~ param_list? ~ ")" ~ ("->" ~ type_expr)? ~ block }
|
||||||
|
|
||||||
struct_field = { identifier ~ ":" ~ type_expr }
|
struct_field = { identifier ~ ":" ~ type_expr }
|
||||||
@@ -23,7 +26,14 @@ param = { identifier ~ ":" ~ type_expr }
|
|||||||
|
|
||||||
// statements
|
// statements
|
||||||
block = { "{" ~ statement* ~ "}" }
|
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 }
|
assign_stmt = { identifier ~ "=" ~ expression }
|
||||||
let_stmt = { "let" ~ identifier ~ (":" ~ type_expr)? ~ "=" ~ expression }
|
let_stmt = { "let" ~ identifier ~ (":" ~ type_expr)? ~ "=" ~ expression }
|
||||||
@@ -31,29 +41,49 @@ return_stmt = { "return" ~ expression? }
|
|||||||
if_stmt = { "if" ~ expression ~ block ~ ("else" ~ block)? }
|
if_stmt = { "if" ~ expression ~ block ~ ("else" ~ block)? }
|
||||||
for_stmt = { "for" ~ identifier ~ "in" ~ expression ~ block }
|
for_stmt = { "for" ~ identifier ~ "in" ~ expression ~ block }
|
||||||
|
|
||||||
// types — optional is a suffix, not a recursive wrapper
|
// types
|
||||||
type_expr = { base_type ~ "?"? }
|
type_expr = { base_type ~ "?"? }
|
||||||
base_type = _{ array_type | identifier }
|
base_type = _{ array_type | identifier }
|
||||||
array_type = { "[" ~ type_expr ~ "]" }
|
array_type = { "[" ~ type_expr ~ "]" }
|
||||||
|
|
||||||
// expressions
|
// expressions (still simple, no precedence yet)
|
||||||
expression = { term ~ (bin_op ~ term)* }
|
expression = { term ~ (bin_op ~ term)* }
|
||||||
bin_op = _{ add | sub | mul | div | eq | neq | lte | gte | lt | gt | and | or }
|
|
||||||
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 = { "*" }
|
||||||
|
div = { "/" }
|
||||||
|
eq = { "==" }
|
||||||
|
neq = { "!=" }
|
||||||
|
lte = { "<=" }
|
||||||
|
gte = { ">=" }
|
||||||
|
lt = { "<" }
|
||||||
|
gt = { ">" }
|
||||||
|
and = { "&&" }
|
||||||
|
or = { "||" }
|
||||||
|
|
||||||
|
// literals
|
||||||
struct_literal = {
|
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 = {
|
struct_init_field = {
|
||||||
@@ -61,12 +91,29 @@ struct_init_field = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
array_literal = {
|
array_literal = {
|
||||||
"[" ~ (expression ~ ("," ~ expression)*)? ~ "]"
|
"[" ~ (expression ~ ("," ~ expression)*)? ~ ","? ~ "]"
|
||||||
}
|
}
|
||||||
|
|
||||||
term = { primary ~ (field_access | call_suffix)* }
|
// expressions continuation
|
||||||
field_access = { "." ~ identifier }
|
term = { primary ~ (field_access | call_suffix)* }
|
||||||
call_suffix = { "(" ~ (expression ~ ("," ~ expression)*)? ~ ")" }
|
|
||||||
primary = _{ struct_literal | array_literal | float | integer | string_lit | boolean | self_kw | null_kw | identifier }
|
field_access = { "." ~ identifier }
|
||||||
self_kw = { "self" }
|
|
||||||
null_kw = { "null" }
|
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" }
|
||||||
|
|||||||
Reference in New Issue
Block a user