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
David Söderberg
D7050E_2020
Commits
93c15c7c
Commit
93c15c7c
authored
4 years ago
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
input programs added
parent
e107b53f
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/ast/ast.rs
+2
-3
2 additions, 3 deletions
src/ast/ast.rs
src/ast/main.rs
+8
-2
8 additions, 2 deletions
src/ast/main.rs
src/main.rs
+1
-2
1 addition, 2 deletions
src/main.rs
tests/w1_2.rs
+72
-0
72 additions, 0 deletions
tests/w1_2.rs
with
83 additions
and
7 deletions
src/ast/ast.rs
+
2
−
3
View file @
93c15c7c
...
...
@@ -11,7 +11,6 @@ pub enum NumOrId {
// println!("{}", ..)
impl
fmt
::
Display
for
NumOrId
{
fn
fmt
(
&
self
,
f
:
&
mut
fmt
::
Formatter
)
->
fmt
::
Result
{
match
self
{
NumOrId
::
Num
(
i
)
=>
write!
(
f
,
"{}"
,
i
)
?
,
...
...
This diff is collapsed.
Click to expand it.
src/ast/main.rs
+
8
−
2
View file @
93c15c7c
...
...
@@ -17,6 +17,12 @@ fn main() {
#[test]
fn
parse_num_or_id
()
{
assert_eq!
(
format!
(
"{}"
,
NumOrIdParser
::
new
()
.parse
(
"123"
)
.unwrap
()),
"123"
);
assert_eq!
(
format!
(
"{}"
,
NumOrIdParser
::
new
()
.parse
(
"a1_a"
)
.unwrap
()),
"a1_a"
);
assert_eq!
(
format!
(
"{}"
,
NumOrIdParser
::
new
()
.parse
(
"123"
)
.unwrap
()),
"123"
);
assert_eq!
(
format!
(
"{}"
,
NumOrIdParser
::
new
()
.parse
(
"a1_a"
)
.unwrap
()),
"a1_a"
);
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
1
−
2
View file @
93c15c7c
...
...
@@ -12,4 +12,3 @@ fn main() {
fn
hello
()
{
println!
(
"Parse an Id {:?}"
,
IdParser
::
new
()
.parse
(
"abcd"
));
}
This diff is collapsed.
Click to expand it.
tests/w1_2.rs
0 → 100644
+
72
−
0
View file @
93c15c7c
// A set of small examples that your parser should accept and parse into an ast
// you should also be able to print the ast (debug ":?" or even prettier using display "{}")
//
// Notice that a sequence of statements may have an optional trailing ";"
fn
_let_and_return
()
{
// a function taking no arguments returning the unit type
fn
a
()
->
()
{
let
_a
:
i32
=
5
;
// this returns a unit type
}
// a function taking two i32 arguments returning the i32 type
fn
b
(
_x
:
i32
,
_y
:
i32
)
->
i32
{
3
// this returns 3 (as i32)
}
// a function taking two i32 arguments returning the i32 type
// with some let statements
fn
c
(
x
:
i32
,
y
:
i32
)
->
i32
{
let
a
:
i32
=
5
;
let
b
:
i32
=
x
+
y
;
// this will be an infix operator "+""
-
a
-
(
-
b
)
*
y
// here we have prefix operator "-"
}
}
// More advanced statements
fn
_if_then_else_and_while
()
{
// a function taking two bool arguments returning the bool type
// with some let statements and function calls
fn
a
(
x
:
bool
,
y
:
bool
)
->
bool
{
if
x
&&
y
{
let
a
:
bool
=
true
;
y
||
a
}
else
{
x
&&
false
}
}
// a function taking two bool arguments returning the i32 type
// with some let statements and function calls
fn
b
(
x
:
bool
,
y
:
bool
)
->
i32
{
let
a
:
bool
=
a
(
x
,
y
||
false
);
let
mut
b
:
i32
=
0
;
if
a
&&
y
{
let
a
:
bool
=
true
;
// shadowing
if
y
||
a
{
b
=
b
+
1
;
};
}
else
{
if
!
(
x
&&
false
)
{
b
=
b
-
1
;
}
};
b
+
3
}
// a function taking two bool arguments returning the i32 type
// while
fn
c
(
x
:
bool
,
y
:
bool
)
->
i32
{
let
mut
b
:
i32
=
0
;
let
mut
c
:
i32
=
1
;
while
(
b
<
10
)
{
c
=
c
*
2
;
}
c
}
}
// optionally you may support other integer types, such as u8, u16, u32, u64, i8, i16, i64 and usize
// you may also support explicit local scopes
//
// later we will introduce references and user defined data types
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