diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs new file mode 100644 index 0000000..722bec1 --- /dev/null +++ b/src/compiler/mod.rs @@ -0,0 +1,8 @@ +use crate::parser::{DeimosParser, Rule}; +use crate::vm::lib::DmLib; +use pest::Parser; + +pub fn compile(src_unit: &str) -> DmLib { + let p = DeimosParser::parse(Rule::compilation_unit, src_unit).unwrap(); + todo!() +} diff --git a/src/lib.rs b/src/lib.rs index 8d82b86..6661955 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +mod compiler; pub mod parser; mod util; pub mod vm; diff --git a/src/parser/deimos.pest b/src/parser/deimos.pest index f11bbbb..a7de239 100644 --- a/src/parser/deimos.pest +++ b/src/parser/deimos.pest @@ -1,24 +1,31 @@ compilation_unit = { SOI ~ namespace? ~ declaration* ~ EOI } namespace = { "ns" ~ fqn } -fqn = { identifier ~ ( "::" ~ fqn )* } +fqn = { identifier ~ ( "::" ~ identifier )* } identifier = @{ identifier_start_char ~ identifier_char* } identifier_start_char = { 'a'..'z' | 'A'..'Z' | "_" } identifier_char = { 'a'..'z' | 'A'..'Z' | '0'..'9' | "_" } -declaration = { "decl"? ~ "pub"? ~ ( interface | module | property | function ) } +declaration = { decl? ~ pub? ~ ( interface | implementation | module | function ) } +decl = { "decl" } +pub = { "pub" } -interface = { "int" ~ identifier ~ generics_declaration? ~ interface_extends_list? ~ "{" ~ declaration* ~ "}"} -interface_extends_list = { ":" ~ type ~ ( "+" ~ type )* } +interface = { "int" ~ identifier ~ generics_declaration? ~ extends_list? ~ ( "{" ~ declaration* ~ "}" )? } +extends_list = { ":" ~ type ~ ( "+" ~ type )* } + +implementation = { "impl" ~ identifier ~ generics_declaration? ~ impl_ctor? ~ extends_list? } +impl_ctor = { "(" ~ impl_ctor_args? ~ ")" } +impl_ctor_args = { impl_ctor_arg ~ ( "," ~ impl_ctor_arg )* } +impl_ctor_arg = { fld? ~ identifier ~ ":" ~ type } +fld = { "fld" } module = { "mod" ~ identifier ~ "{" ~ declaration* ~ "}" } property = { identifier ~ ":" ~ type } -function = { "fn" ~ generics_declaration? ~ identifier ~ "(" ~ args_list? ~ ")" ~ ( ":" ~ type )? ~ ( "{" ~ "}" )? } - +function = { "fn" ~ generics_declaration? ~ identifier ~ "(" ~ args_list? ~ ")" ~ ( ":" ~ type )? ~ ( function_body | function_equals_body ) } function_equals_body = { "=" ~ expression } -function_body = { "{" ~ "}" } +function_body = { "{" ~ statement* ~ "}" } type = { identifier ~ generics_declaration? } generics_declaration = { "<" ~ identifier ~ ( "," ~ identifier )* ~ ">"} @@ -26,12 +33,29 @@ generics_declaration = { "<" ~ identifier ~ ( "," ~ identifier )* ~ ">"} args_list = { arg ~ ( "," ~ arg )* } arg = { identifier ~ ( ":" ~ "..."? ~ type )? } -expression = { literal } +statement = { call_stmt } -literal = { number_literal } +call_stmt = { call_expr } + +call_expr = { call_expr_with_parens | call_expr_no_parens } +call_expr_with_parens = { fqn ~ "(" ~ call_args? ~ ")" } +call_expr_no_parens = { fqn ~ call_args } +call_args = { call_arg ~ ( "," ~ call_arg )* } +call_arg = { expression } + +expression = { literal | identifier } + +literal = { number_literal | string_literal } number_literal = { int_literal } - int_literal = { '0'..'9'+ } -WHITESPACE = { " " | "\t" | "\n" | "\r" } \ No newline at end of file +string_literal = ${ "\"" ~ string_inner ~ "\"" } +string_inner = @{ string_char* } +string_char = { + !( "\"" | "\\" ) ~ ANY + | "\\" ~ ( "\"" | "\\" | "/" | "b" | "f" | "n" | "r" | "t" ) + | "\\" ~ ( "u" ~ ASCII_HEX_DIGIT{4} ) +} + +WHITESPACE = _{ " " | "\t" | "\n" | "\r" } \ No newline at end of file