Working c like parsing

This commit is contained in:
2026-04-22 16:23:59 +02:00
parent 2576c77466
commit 4f2d1d3664
4 changed files with 29 additions and 32 deletions
+3 -3
View File
@@ -17,19 +17,19 @@ top_level = _{ import | function_decl | struct_decl | package }
package = { "package" ~ identifier ~ ";" }
export = { "export" }
export = { "public" }
import = { "import" ~ string_lit ~ ";" }
param_list = { param ~ ("," ~ param)* ~ ","? }
param = { export? ~ identifier ~ ":" ~ type_expr }
param = { export? ~ type_expr ~ identifier }
struct_decl = {
export? ~ "struct" ~ identifier ~ "{" ~ param_list? ~ "}"
}
function_decl = {
export? ~ "function" ~ identifier ~ "(" ~ param_list? ~ ")" ~ (":" ~ type_expr)? ~ block
export? ~ type_expr ~ identifier ~ "(" ~ param_list? ~ ")" ~ block
}
// ================= STATEMENTS =================
+21 -13
View File
@@ -29,16 +29,22 @@ pub fn parse(source: &str) -> Result<Vec<TopLevel>, ParseError> {
Ok(statements)
}
impl From<pest::iterators::Pair<'_, Rule>> for TypeExpr {
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
match pair.as_rule() {
impl TryFrom<pest::iterators::Pair<'_, Rule>> for TypeExpr {
type Error = ();
fn try_from(pair: pest::iterators::Pair<'_, Rule>) -> Result<Self, Self::Error> {
if pair.as_str() == "void" {
return Err(());
}
Ok(match pair.as_rule() {
Rule::type_expr => {
let inner = pair.into_inner().next().unwrap();
TypeExpr::from(inner)
TypeExpr::try_from(inner)?
}
Rule::identifier => TypeExpr::Identifier(pair.as_str().to_string()),
_ => unimplemented!("TypeExpr parsing not implemented yet"),
}
})
}
}
@@ -55,8 +61,8 @@ impl From<(bool, pest::iterators::Pair<'_, Rule>)> for ParamList {
} else {
false
};
let param_type = TypeExpr::try_from(param_inner.next().unwrap()).unwrap();
let param_name = param_inner.next().unwrap().as_str().to_string();
let param_type = TypeExpr::from(param_inner.next().unwrap());
(param_name, (export, param_type))
})
.collect();
@@ -90,15 +96,10 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
} else {
false
};
let name = inner.next().unwrap().as_str().to_string();
let params = if inner.peek().unwrap().as_rule() == Rule::param_list {
ParamList::from((false, inner.next().unwrap()))
} else {
ParamList(HashMap::new())
};
let return_type = if let Some(next) = inner.peek() {
if next.as_rule() == Rule::type_expr {
Some(TypeExpr::from(inner.next().unwrap()))
TypeExpr::try_from(inner.next().unwrap()).ok()
} else {
None
}
@@ -106,6 +107,13 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
None
};
let name = inner.next().unwrap().as_str().to_string();
let params = if inner.peek().unwrap().as_rule() == Rule::param_list {
ParamList::from((false, inner.next().unwrap()))
} else {
ParamList(HashMap::new())
};
let body = Block::from(inner.next().unwrap());
Ok(TopLevel::FunctionDecl {
+1 -1
View File
@@ -1,5 +1,5 @@
{
"entry": "src/main.ms",
"out_dir": "build",
"script": true
"script": false
}
+4 -15
View File
@@ -2,21 +2,10 @@ package main;
import "fmt";
function main() {
doSomething();
let a = doSomethingElse();
if (a + 1 == 67 + 1) {
fmt.println(a);
}
void main() {
test(67);
}
function doSomething() {
fmt.println("hello");
}
function doSomethingElse(): int {
fmt.println("world");
return 67;
void test(int x) {
fmt.println("Hello" + x);
}