Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
erode
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Per Lindgren
erode
Commits
bb2c8418
Commit
bb2c8418
authored
4 years ago
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
move to new syntax, wip1
parent
2f1cb57d
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
examples/let_if.rs
+4
-0
4 additions, 0 deletions
examples/let_if.rs
src/ast.rs
+4
-0
4 additions, 0 deletions
src/ast.rs
src/check.rs
+4
-0
4 additions, 0 deletions
src/check.rs
src/grammar.lalrpop
+83
-44
83 additions, 44 deletions
src/grammar.lalrpop
tests/test_check.rs
+5
-0
5 additions, 0 deletions
tests/test_check.rs
with
100 additions
and
44 deletions
examples/let_if.rs
0 → 100644
+
4
−
0
View file @
bb2c8418
fn
main
()
{
//let a: i32 = if true { 1 } else { 2 };
let
a
=
5
;
}
This diff is collapsed.
Click to expand it.
src/ast.rs
+
4
−
0
View file @
bb2c8418
...
@@ -94,6 +94,8 @@ pub enum Expr {
...
@@ -94,6 +94,8 @@ pub enum Expr {
RefMut
(
Box
<
Expr
>
),
RefMut
(
Box
<
Expr
>
),
DeRef
(
Box
<
Expr
>
),
DeRef
(
Box
<
Expr
>
),
Call
(
Id
,
Exprs
),
Call
(
Id
,
Exprs
),
Block
(
Stmts
),
Stmt
(
Stmt
),
}
}
#[derive(Debug,
PartialEq,
Clone)]
#[derive(Debug,
PartialEq,
Clone)]
...
@@ -319,6 +321,8 @@ impl Display for Expr {
...
@@ -319,6 +321,8 @@ impl Display for Expr {
RefMut
(
ref
e
)
=>
write!
(
fmt
,
"&mut {}"
,
e
),
RefMut
(
ref
e
)
=>
write!
(
fmt
,
"&mut {}"
,
e
),
DeRef
(
ref
e
)
=>
write!
(
fmt
,
"*{}"
,
e
),
DeRef
(
ref
e
)
=>
write!
(
fmt
,
"*{}"
,
e
),
Call
(
ref
s
,
ref
exprs
)
=>
write!
(
fmt
,
"{}{}"
,
s
,
exprs
),
Call
(
ref
s
,
ref
exprs
)
=>
write!
(
fmt
,
"{}{}"
,
s
,
exprs
),
Block
(
ref
s
)
=>
write!
(
fmt
,
"{}"
,
s
),
Stmt
(
ref
s
)
=>
write!
(
fmt
,
"{}"
,
s
),
}
}
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
src/check.rs
+
4
−
0
View file @
bb2c8418
...
@@ -187,6 +187,10 @@ fn expr_type(
...
@@ -187,6 +187,10 @@ fn expr_type(
_
=>
Err
(
format!
(
"cannot deref {} of type {}"
,
e
,
t
)),
_
=>
Err
(
format!
(
"cannot deref {} of type {}"
,
e
,
t
)),
}
}
}
}
Block
(
b
)
=>
panic!
(),
Stmt
(
s
)
=>
panic!
(),
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
src/grammar.lalrpop
+
83
−
44
View file @
bb2c8418
...
@@ -98,7 +98,7 @@ Variant : Variant = {
...
@@ -98,7 +98,7 @@ Variant : Variant = {
}
}
pub FnDecl : FnDecl = {
pub FnDecl : FnDecl = {
"fn" <id: Id> <params: Params> <result: ("->" <Type>)?>
"{"
<body:
Stmts> "}"
"fn" <id: Id> <params: Params> <result: ("->" <Type>)?> <body:
Block>
=> FnDecl {
=> FnDecl {
id,
id,
params,
params,
...
@@ -126,29 +126,6 @@ Param : Param = {
...
@@ -126,29 +126,6 @@ Param : Param = {
},
},
}
}
pub Stmts: Stmts = {
<s: (<Stmt> ";")*> <e: Stmt?> => match e {
// stmts return ()
None => Stmts { stmts: s, ret: true },
// last statement is the return value
Some(e) => {
let mut s = s;
s.push(e);
Stmts { stmts: s, ret: false }
},
}
}
Stmt : Stmt = {
"let" <m: "mut"?> <id: Id> <t: (":" <Type>)?> <e: ("=" <Expr>)?> => Stmt::Let(id, m.is_some(), t, e),
<Expr> "=" <Expr> => Stmt::Assign(<>),
"while" <Expr> "{" <Stmts> "}" => Stmt::While(<>),
"if" <Expr> "{" <Stmts> "}" < ("else" "{" <Stmts> "}")?> => Stmt::If(<>),
<Expr> => Stmt::Expr(<>),
"{" <Stmts> "}" => Stmt::Block(<>),
}
Type : Type = {
Type : Type = {
"bool" => Type::Bool,
"bool" => Type::Bool,
"()" => Type::Unit,
"()" => Type::Unit,
...
@@ -158,16 +135,79 @@ Type : Type = {
...
@@ -158,16 +135,79 @@ Type : Type = {
"&" "mut" <Type> => Type::Ref(Box::new(Type::Mut(Box::new(<>)))),
"&" "mut" <Type> => Type::Ref(Box::new(Type::Mut(Box::new(<>)))),
}
}
// pub Stmts: Stmts = {
// <s: (<Stmt> ";")*> <e: Stmt?> => match e {
// // stmts return ()
// None => Stmts { stmts: s, ret: true },
// // last statement is the return value
// Some(e) => {
// let mut s = s;
// s.push(e);
// Stmts { stmts: s, ret: false }
// },
// }
// }
pub Block: Stmts = {
"{" <Stmt*> "}" => Stmts { stmts: <>, ret: false },
}
// pub Stmt: () = {
// ";" => (),
// "let" "mut"? Id "=" Expr ";" => (),
// ExprNoBlock "=" Expr => (),
// "while" Expr Block => (),
// "if" Expr Block ("else" Block)? => (),
// ExprNoBlock => (),
// Block => (),
// }
Stmt : Stmt = {
";" => Stmt::Semi,
//"let" <m: "mut"?> <id: Id> <t: (":" <Type>)?> <e: ("=" <Expr>)?> => Stmt::Let(id, m.is_some(), t, e),
// <Expr> "=" <Expr> => Stmt::Assign(<>),
// "while" <Expr> "{" <Block> "}" => Stmt::While(<>),
// "if" <Expr> <Block> < ("else" <Block>)?> => Stmt::If(<>),
<ExprNoBlock> => Stmt::Expr(<>),
// <Block> => Stmt::Block(<>),
}
pub Exprs : Exprs = {
pub Exprs : Exprs = {
ParCNT<Expr> => Exprs( <> ),
ParCNT<ExprNoBlock> => Exprs( <> ),
};
}
pub Expr = {
ExprBlock,
ExprNoBlock,
}
pub ExprBlock: Box<Expr> = {
"if" <e: Expr> <tb: Block> "else" <eb: Block> => {
let s = Stmt::If(e, tb, Some(eb));
Box::new(Expr::Stmt(s))
},
Block => Box::new(Expr::Block(<>)),
}
pub ExprNoBlock: Box<Expr> = {
// ExprNoBlock Cmp Expr1 => (),
Expr1,
}
// Not sure about the precedence
// Not sure about the precedence
pub Expr: Box<Expr> = {
pub Expr
1
: Box<Expr> = {
Expr BoolOp
Factor
=> Box::new(Expr::Infix(<>)),
Expr
1
BoolOp
Expr2
=> Box::new(Expr::Infix(<>)),
Expr AddSubOp
Factor
=> Box::new(Expr::Infix(<>)),
Expr
1
AddSubOp
Expr2
=> Box::new(Expr::Infix(<>)),
<Expr> "as" <Type> => Box::new(Expr::As(<>)),
<Expr
1
> "as" <Type> => Box::new(Expr::As(<>)),
Factor
,
Expr2
,
};
};
AddSubOp: Op = {
AddSubOp: Op = {
...
@@ -186,8 +226,8 @@ BoolOp: Op = {
...
@@ -186,8 +226,8 @@ BoolOp: Op = {
"&&" => Op::And,
"&&" => Op::And,
};
};
Factor
: Box<Expr> = {
Expr2
: Box<Expr> = {
Factor
MulDivOp Term => Box::new(Expr::Infix(<>)),
Expr2
MulDivOp Term => Box::new(Expr::Infix(<>)),
Term,
Term,
};
};
...
@@ -198,17 +238,16 @@ MulDivOp: Op = {
...
@@ -198,17 +238,16 @@ MulDivOp: Op = {
Term: Box<Expr> = {
Term: Box<Expr> = {
Num => Box::new(Expr::Num(<>)),
Num => Box::new(Expr::Num(<>)),
"true" => Box::new(Expr::Bool(true)),
// "true" => Box::new(Expr::Bool(true)),
"false" => Box::new(Expr::Bool(false)),
// "false" => Box::new(Expr::Bool(false)),
<Id> <Exprs> => Box::new(Expr::Call(<>)),
// <Id> <Exprs> => Box::new(Expr::Call(<>)),
Id => Box::new(Expr::Id(<>)),
// Id => Box::new(Expr::Id(<>)),
AddSubOp Term => Box::new(Expr::Prefix(<>)),
// AddSubOp Term => Box::new(Expr::Prefix(<>)),
"!" <Term> => Box::new(Expr::Prefix(Op::Not, <>)),
// "!" <Term> => Box::new(Expr::Prefix(Op::Not, <>)),
"&" <Term> => Box::new(Expr::Ref(<>)),
// "&" <Term> => Box::new(Expr::Ref(<>)),
"&" "mut" <Term> => Box::new(Expr::RefMut(<>)),
// "&" "mut" <Term> => Box::new(Expr::RefMut(<>)),
"*" <Term> => Box::new(Expr::DeRef(<>)),
// "*" <Term> => Box::new(Expr::DeRef(<>)),
"(" <Expr> ")"
// "(" <Expr> ")"
// "{{" <Stmt> "}} "=> Box::new(Expr::Stmt(<>))
};
};
Num: i32 = {
Num: i32 = {
...
...
This diff is collapsed.
Click to expand it.
tests/test_check.rs
+
5
−
0
View file @
bb2c8418
...
@@ -169,3 +169,8 @@ fn check_scopes() {
...
@@ -169,3 +169,8 @@ fn check_scopes() {
fn
check_scopes_err
()
{
fn
check_scopes_err
()
{
assert!
(
check
(
&
read_file
::
parse
(
"examples/scopes_err.rs"
))
.is_err
());
assert!
(
check
(
&
read_file
::
parse
(
"examples/scopes_err.rs"
))
.is_err
());
}
}
#[test]
fn
check_let_if
()
{
assert!
(
check
(
&
read_file
::
parse
(
"examples/let_if.rs"
))
.is_err
());
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment