From f99968c638fa5ee4896ef8c3a832886d1c98cc5a Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Mon, 13 Apr 2026 16:23:17 +0200 Subject: [PATCH] Typescript-like syntax --- src/parser/grammar.pest | 45 ++++++++++++++++++++++++++++------------- src/parser/mod.rs | 2 -- test/src/main.ms | 10 ++++++++- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/parser/grammar.pest b/src/parser/grammar.pest index aaee271..8a59ca2 100644 --- a/src/parser/grammar.pest +++ b/src/parser/grammar.pest @@ -12,31 +12,37 @@ boolean = { "true" | "false" } string_lit = { "\"" ~ inner_str ~ "\"" } inner_str = @{ (!"\"" ~ ANY)* } -// top level +// ================= TYPE SCRIPT STYLE CHANGES ================= + +// import becomes TS-like (still identical structure) import_decl = { "import" ~ string_lit ~ ";" } +// struct stays, but feels like TS "type object shape" struct_decl = { "struct" ~ identifier ~ "{" ~ struct_field* ~ "}" } +// class already TS-like — just improved consistency class_decl = { "class" ~ identifier ~ "{" ~ class_member* ~ "}" } class_member = _{ struct_field | function_decl } +// function keyword changed feel: fn → function function_decl = { - "fn" ~ identifier ~ "(" ~ param_list? ~ ")" ~ ("->" ~ type_expr)? ~ block + "function" ~ identifier ~ "(" ~ param_list? ~ ")" ~ ("->" ~ type_expr)? ~ block } -// struct fields (NO commas, Go-style) +// struct fields: TS-style semicolon consistency (already good) struct_field = { identifier ~ ":" ~ type_expr ~ ";" } -// params (allow trailing comma) +// params: TS optional typing style feel (unchanged structure) param_list = { param ~ ("," ~ param)* ~ ","? } param = { identifier ~ ":" ~ type_expr } -// statements +// ================= STATEMENTS ================= + block = { "{" ~ statement* ~ "}" } statement = _{ @@ -48,30 +54,39 @@ statement = _{ | expression_stmt } +// expression statements: TS allows no semicolon in real life, +// but we keep your structure expression_stmt = { expression ~ ";" } +// assignment unchanged (JS/TS feel already) assign_stmt = { identifier ~ "=" ~ expression ~ ";" } +// let → const/let feel (still your rule, just TS naming) let_stmt = { - "let" ~ identifier ~ (":" ~ type_expr)? ~ "=" ~ expression ~ ";" + "const" ~ identifier ~ (":" ~ type_expr)? ~ "=" ~ expression ~ ";" } +// return: TS-like (no change except feel consistency) return_stmt = { "return" ~ expression? ~ ";" } +// if: add TS-style parentheses (MAJOR feel upgrade, same structure) if_stmt = { - "if" ~ expression ~ block ~ ("else" ~ block)? + "if" ~ "(" ~ expression ~ ")" ~ block ~ ("else" ~ block)? } +// for: TS-like "of" for_stmt = { - "for" ~ identifier ~ "in" ~ expression ~ block + "for" ~ identifier ~ "of" ~ expression ~ block } -// types +// ================= TYPES (KEEP STRUCTURE, TS FEEL) ================= + type_expr = { base_type ~ "?"? } base_type = _{ array_type | identifier } array_type = { "[" ~ type_expr ~ "]" } -// expressions (still no precedence yet) +// ================= EXPRESSIONS ================= + expression = { term ~ (bin_op ~ term)* } bin_op = _{ @@ -102,7 +117,7 @@ gt = { ">" } and = { "&&" } or = { "||" } -// literals +// ================= LITERALS ================= struct_literal = { identifier ~ "{" ~ struct_init_list? ~ "}" @@ -120,7 +135,8 @@ array_literal = { "[" ~ (expression ~ ("," ~ expression)*)? ~ ","? ~ "]" } -// chaining +// ================= CHAINING ================= + term = { primary ~ (field_access | call_suffix)* } field_access = { "." ~ identifier } @@ -129,7 +145,8 @@ call_suffix = { "(" ~ (expression ~ ("," ~ expression)*)? ~ ","? ~ ")" } -// primary +// ================= PRIMARY ================= + primary = _{ struct_literal | array_literal @@ -142,5 +159,5 @@ primary = _{ | identifier } -self_kw = { "self" } +self_kw = { "this" } null_kw = { "null" } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 8be2197..a8ad54b 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -15,8 +15,6 @@ pub type ParseError = pest::error::Error; pub fn parse(source: &str) -> Result { let pairs = MistParser::parse(Rule::program, source)?; - println!("Parsed pairs: {:#?}", pairs); - let mut statements = vec![]; // pairs is an iterator over the top-level program pair diff --git a/test/src/main.ms b/test/src/main.ms index cf8f728..2d85d4a 100644 --- a/test/src/main.ms +++ b/test/src/main.ms @@ -1,5 +1,13 @@ import "fmt"; -fn main() { +function add(a: int, b: int) -> int { + const x = 10; + if (x > 5) { + return x; + } + return 0; +} + +function main() { fmt.Println("Hello World!"); } \ No newline at end of file