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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Per Lindgren
D7050E_2020
Commits
93adf16b
Commit
93adf16b
authored
4 years ago
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
passes simple test
parent
eabfc31b
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples/if.rs
+6
-0
6 additions, 0 deletions
examples/if.rs
src/ast/main.rs
+27
-5
27 additions, 5 deletions
src/ast/main.rs
src/ast/parser.lalrpop
+48
-20
48 additions, 20 deletions
src/ast/parser.lalrpop
with
81 additions
and
25 deletions
examples/if.rs
0 → 100644
+
6
−
0
View file @
93adf16b
fn
main
()
{
let
a
=
{
let
b
=
1
;
b
};
}
This diff is collapsed.
Click to expand it.
src/ast/main.rs
+
27
−
5
View file @
93adf16b
use
std
::
fs
::
File
;
use
std
::
io
::
prelude
::
*
;
use
lalrpop_util
::
lalrpop_mod
;
use
lalrpop_util
::
lalrpop_mod
;
lalrpop_mod!
(
pub
parser
,
"/ast/parser.rs"
);
lalrpop_mod!
(
pub
parser
,
"/ast/parser.rs"
);
...
@@ -7,13 +10,32 @@ use parser::*;
...
@@ -7,13 +10,32 @@ use parser::*;
pub
mod
ast
;
pub
mod
ast
;
use
ast
::
*
;
use
ast
::
*
;
fn
main
()
{
fn
main
()
{}
pub
fn
read
(
file_name
:
&
str
)
->
std
::
io
::
Result
<
String
>
{
let
mut
file
=
File
::
open
(
file_name
)
?
;
let
mut
contents
=
String
::
new
();
file
.read_to_string
(
&
mut
contents
)
?
;
Ok
(
contents
)
}
// pub fn parse(file_name: &str) -> Program {
// let p = read(file_name).expect("File not found");
// ProgramParser::new().parse(&p).unwrap()
// }
#[test]
fn
t1
()
{
let
s
=
"
let
s
=
"
{
{
let ;
let a = {
let b = 1 + 1;
;
;
if a { }
b
}
}
}
"
;
"
;
StmtsParser
::
new
()
.parse
(
s
)
.unwrap
();
println!
(
"{:?}"
,
BlockExpressionParser
::
new
()
.parse
(
s
));
}
}
This diff is collapsed.
Click to expand it.
src/ast/parser.lalrpop
+
48
−
20
View file @
93adf16b
...
@@ -4,40 +4,68 @@ use crate::ast::*;
...
@@ -4,40 +4,68 @@ use crate::ast::*;
grammar;
grammar;
pub ExpressionWithoutBlock: () = {
match {
LiteralExpression => ()
// The default whitespace skipping is disabled an `ignore pattern` is specified
r"\s*" => { },
// Skip `// comments`
r"//[^\n\r]*" => { },
// Skip `/* comments */`
r"/\*([^\*]*\*+[^\*/])*([^\*]*\*+|[^\*])*\*/" => { },
_
}
}
pub LiteralExpression = {
//pub Expr: ()
Num
pub Expr: () = {
Id
ExprStmt => (),
ExprNoBlock => (),
}
}
ExpressionWithBlock: () = {
pub ExprNoBlock: () = {
BlockExpression => ()
Expr AddSub Factor => (),
Factor => (),
}
}
pub BlockExpression: ()= {
pub AddSub: () = {
"{" Statements? "}" => ()
"+" => (),
"-" => (),
}
}
pub Statements: () = {
pub Factor: () = {
Statement+ => (),
Factor MulDiv Term => (),
Statement+ ExpressionWithoutBlock => (),
Term => (),
ExpressionWithBlock => ()
}
}
pub Statement: () = {
pub MulDiv: () = {
";" => (),
"/" => (),
"let" ";" => (),
"*" => (),
ExpressionStatement => ()
}
pub Term: () = {
Id => (),
Num => (),
}
pub ExprStmt: () = {
"if" Expr Stmts "else" Stmts => (),
Stmts => (),
}
pub Stmts: () = {
"{" Stmt* "}" => (),
}
}
pub ExpressionStatement: () = {
pub Stmt: () = {
ExpressionWithoutBlock ";" => (),
";" => (),
ExpressionWithBlock ";"? => ()
"let" Id "=" Expr => (),
"if" Expr Stmts ("else" Stmts)? => (),
ExprNoBlock => (),
Stmts => (),
}
}
pub Num: i32 = {
pub Num: i32 = {
r"[0-9]+" => i32::from_str(<>).unwrap(),
r"[0-9]+" => i32::from_str(<>).unwrap(),
};
};
...
...
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