Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
D7050E_2020
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
D7050E_2020
Commits
58eac48f
Commit
58eac48f
authored
4 years ago
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
syntax tests pass, polish
parent
262e02bf
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples/syntax3.rs
+26
-1
26 additions, 1 deletion
examples/syntax3.rs
src/ast/main.rs
+1
-2
1 addition, 2 deletions
src/ast/main.rs
src/ast/parser.lalrpop
+55
-50
55 additions, 50 deletions
src/ast/parser.lalrpop
with
82 additions
and
53 deletions
examples/syntax3.rs
+
26
−
1
View file @
58eac48f
fn
main
()
{
fn
main
()
{}
fn
a
(
mut
a
:
i32
)
{
let
a
=
5
;
let
a
=
5
;
let
b
=
{
5
};
let
b
=
{
5
};
}
}
fn
b
(
x
:
bool
)
->
i32
{
if
x
{
1
}
else
{
2
}
}
fn
c
()
{
b
(
false
);
let
b
=
if
true
{
let
mut
a
=
1
;
while
a
>
0
{
a
=
a
-
1
}
a
}
else
{
{
5
}
};
}
This diff is collapsed.
Click to expand it.
src/ast/main.rs
+
1
−
2
View file @
58eac48f
...
@@ -8,7 +8,6 @@ lalrpop_mod!(pub parser, "/ast/parser.rs");
...
@@ -8,7 +8,6 @@ lalrpop_mod!(pub parser, "/ast/parser.rs");
use
parser
::
*
;
use
parser
::
*
;
pub
mod
ast
;
pub
mod
ast
;
use
ast
::
*
;
fn
main
()
{}
fn
main
()
{}
...
@@ -21,7 +20,7 @@ pub fn read(file_name: &str) -> std::io::Result<String> {
...
@@ -21,7 +20,7 @@ pub fn read(file_name: &str) -> std::io::Result<String> {
pub
fn
parse
(
file_name
:
&
str
)
{
pub
fn
parse
(
file_name
:
&
str
)
{
let
p
=
read
(
file_name
)
.expect
(
"File not found"
);
let
p
=
read
(
file_name
)
.expect
(
"File not found"
);
Function
Parser
::
new
()
.parse
(
&
p
)
.unwrap
()
Program
Parser
::
new
()
.parse
(
&
p
)
.unwrap
()
}
}
#[test]
#[test]
...
...
This diff is collapsed.
Click to expand it.
src/ast/parser.lalrpop
+
55
−
50
View file @
58eac48f
...
@@ -16,113 +16,118 @@ match {
...
@@ -16,113 +16,118 @@ match {
// A comma separated sequence without trailing comma
// A comma separated sequence without trailing comma
CommaNoTrail<T>: Vec<T> = {
CommaNoTrail<T>: Vec<T> = {
<v:(<T> ",")*> <e:T> => {
<mut v:(<T> ",")*> <e:T> => { v.push(e); v }
let mut v = v;
v.push(e);
v
}
}
Tier<Op,NextTier>: Box<Expr> = {
Tier<Op,NextTier> Op NextTier => Box::new(Expr::Op(<>)),
NextTier
};
pub Program: () = {
Function*
}
}
pub Function: () = {
pub Function: () = {
"fn" Id Params ("->" Type)? Block
=> ()
,
"fn" Id Params ("->" Type)? Block,
}
}
pub Params: () = {
pub Params: () = {
"()"
=> ()
, // seems like a haxx
"()", // seems like a haxx
"("
(
(Param ",")* Param
)
? ")"
=> ()
,
"(" (Param ",")* Param? ")",
}
}
pub Param:() = {
pub Param:() = {
Id ":" Type,
"mut"?
Id ":" Type,
}
}
pub Type:() = {
pub Type:() = {
"i32"
=> ()
,
"i32",
"bool"
=> ()
,
"bool",
"()"
=> ()
,
"()",
}
}
pub Block: () = {
pub Block: () = {
"{" StmtSeq* "}"
=> ()
,
"{" StmtSeq* "}",
"{" StmtSeq* Stmt "}"
=> ()
,
"{" StmtSeq* Stmt "}",
}
}
pub StmtSeq: () = {
pub StmtSeq: () = {
Stmt ";"
=> ()
,
Stmt ";",
StmtBlock
=> ()
,
StmtBlock,
}
}
pub StmtBlock: () = {
pub StmtBlock: () = {
"while" Expr Block
=> ()
,
"while" Expr Block,
"if" Expr Block ("else" Block)?
=> ()
,
"if" Expr Block ("else" Block)?,
Block
=> ()
,
Block,
}
}
pub Stmt: () = {
pub Stmt: () = {
";"
=> ()
,
";",
"let" "mut"? Id "=" Expr
=> ()
,
"let" "mut"? Id "=" Expr,
ExprNoBlock "=" Expr
=> ()
,
ExprNoBlock "=" Expr,
ExprNoBlock
=> ()
,
ExprNoBlock,
}
}
pub Expr: () = {
pub Expr: () = {
ExprBlock
=> ()
,
ExprBlock,
ExprNoBlock
=> ()
,
ExprNoBlock,
}
}
pub ExprBlock: () = {
pub ExprBlock: () = {
"if" ExprNoBlock Block "else" Block
=> ()
,
"if" ExprNoBlock Block "else" Block,
Block
=> ()
,
Block,
}
}
// Here is our ordinary expressions
// Here is our ordinary expressions
pub ExprNoBlock: () = {
pub ExprNoBlock: () = {
ExprNoBlock "||" Expr0
=> ()
,
ExprNoBlock "||" Expr0,
ExprNoBlock "&&" Expr0
=> ()
,
ExprNoBlock "&&" Expr0,
Expr0
=> ()
,
Expr0,
}
}
// Comparison
// Comparison
pub Expr0: () = {
pub Expr0: () = {
Expr0 "==" Expr1
=> ()
,
Expr0 "==" Expr1,
Expr0 "!=" Expr1
=> ()
,
Expr0 "!=" Expr1,
Expr0 ">" Expr1
=> ()
,
Expr0 ">" Expr1,
Expr0 "<" Expr1
=> ()
,
Expr0 "<" Expr1,
Expr1
=> ()
,
Expr1,
}
}
// AddSub
// AddSub
pub Expr1: () = {
pub Expr1: () = {
Expr1 "+" Expr2
=> ()
,
Expr1 "+" Expr2,
Expr1 "-" Expr2
=> ()
,
Expr1 "-" Expr2,
Expr2
=> ()
,
Expr2,
}
}
// MulDiv
// MulDiv
pub Expr2: () = {
pub Expr2: () = {
Expr2 "/" Expr3
=> ()
,
Expr2 "/" Expr3,
Expr2 "*" Expr3
=> ()
,
Expr2 "*" Expr3,
Expr3
=> ()
,
Expr3,
}
}
// Unary
// Unary
pub Expr3: () = {
pub Expr3: () = {
"*" Term
=> ()
,
"*" Term,
"&" Term
=> ()
,
"&" Term,
"&" "mut" Term
=> ()
,
"&" "mut" Term,
"!" Term
=> ()
,
"!" Term,
Term
=> ()
,
Term,
}
}
pub Term: () = {
pub Term: () = {
Id
=> ()
,
Id,
Num
=> ()
,
Num,
Id "(" CommaNoTrail<Expr> ")"
=> ()
,
Id "(" CommaNoTrail<Expr> ")",
"(" Expr ")"
=> ()
,
"(" Expr ")",
}
}
...
...
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