Working c like parsing
This commit is contained in:
@@ -17,19 +17,19 @@ top_level = _{ import | function_decl | struct_decl | package }
|
|||||||
|
|
||||||
package = { "package" ~ identifier ~ ";" }
|
package = { "package" ~ identifier ~ ";" }
|
||||||
|
|
||||||
export = { "export" }
|
export = { "public" }
|
||||||
|
|
||||||
import = { "import" ~ string_lit ~ ";" }
|
import = { "import" ~ string_lit ~ ";" }
|
||||||
|
|
||||||
param_list = { param ~ ("," ~ param)* ~ ","? }
|
param_list = { param ~ ("," ~ param)* ~ ","? }
|
||||||
param = { export? ~ identifier ~ ":" ~ type_expr }
|
param = { export? ~ type_expr ~ identifier }
|
||||||
|
|
||||||
struct_decl = {
|
struct_decl = {
|
||||||
export? ~ "struct" ~ identifier ~ "{" ~ param_list? ~ "}"
|
export? ~ "struct" ~ identifier ~ "{" ~ param_list? ~ "}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function_decl = {
|
function_decl = {
|
||||||
export? ~ "function" ~ identifier ~ "(" ~ param_list? ~ ")" ~ (":" ~ type_expr)? ~ block
|
export? ~ type_expr ~ identifier ~ "(" ~ param_list? ~ ")" ~ block
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= STATEMENTS =================
|
// ================= STATEMENTS =================
|
||||||
|
|||||||
+21
-13
@@ -29,16 +29,22 @@ pub fn parse(source: &str) -> Result<Vec<TopLevel>, ParseError> {
|
|||||||
Ok(statements)
|
Ok(statements)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for TypeExpr {
|
impl TryFrom<pest::iterators::Pair<'_, Rule>> for TypeExpr {
|
||||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
type Error = ();
|
||||||
match pair.as_rule() {
|
|
||||||
|
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 => {
|
Rule::type_expr => {
|
||||||
let inner = pair.into_inner().next().unwrap();
|
let inner = pair.into_inner().next().unwrap();
|
||||||
TypeExpr::from(inner)
|
TypeExpr::try_from(inner)?
|
||||||
}
|
}
|
||||||
Rule::identifier => TypeExpr::Identifier(pair.as_str().to_string()),
|
Rule::identifier => TypeExpr::Identifier(pair.as_str().to_string()),
|
||||||
_ => unimplemented!("TypeExpr parsing not implemented yet"),
|
_ => unimplemented!("TypeExpr parsing not implemented yet"),
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,8 +61,8 @@ impl From<(bool, pest::iterators::Pair<'_, Rule>)> for ParamList {
|
|||||||
} else {
|
} else {
|
||||||
false
|
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_name = param_inner.next().unwrap().as_str().to_string();
|
||||||
let param_type = TypeExpr::from(param_inner.next().unwrap());
|
|
||||||
(param_name, (export, param_type))
|
(param_name, (export, param_type))
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
@@ -90,15 +96,10 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
|||||||
} else {
|
} else {
|
||||||
false
|
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() {
|
let return_type = if let Some(next) = inner.peek() {
|
||||||
if next.as_rule() == Rule::type_expr {
|
if next.as_rule() == Rule::type_expr {
|
||||||
Some(TypeExpr::from(inner.next().unwrap()))
|
TypeExpr::try_from(inner.next().unwrap()).ok()
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@@ -106,6 +107,13 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
|||||||
None
|
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());
|
let body = Block::from(inner.next().unwrap());
|
||||||
|
|
||||||
Ok(TopLevel::FunctionDecl {
|
Ok(TopLevel::FunctionDecl {
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"entry": "src/main.ms",
|
"entry": "src/main.ms",
|
||||||
"out_dir": "build",
|
"out_dir": "build",
|
||||||
"script": true
|
"script": false
|
||||||
}
|
}
|
||||||
+4
-15
@@ -2,21 +2,10 @@ package main;
|
|||||||
|
|
||||||
import "fmt";
|
import "fmt";
|
||||||
|
|
||||||
function main() {
|
void main() {
|
||||||
doSomething();
|
test(67);
|
||||||
let a = doSomethingElse();
|
|
||||||
|
|
||||||
if (a + 1 == 67 + 1) {
|
|
||||||
fmt.println(a);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function doSomething() {
|
void test(int x) {
|
||||||
fmt.println("hello");
|
fmt.println("Hello" + x);
|
||||||
}
|
|
||||||
|
|
||||||
function doSomethingElse(): int {
|
|
||||||
fmt.println("world");
|
|
||||||
|
|
||||||
return 67;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user