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
David Eriksson
D7050E_2020
Commits
b8afed80
Commit
b8afed80
authored
Sep 13, 2020
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
fallible wip1
parent
93c15c7c
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
src/ast/ast.rs
+12
-2
12 additions, 2 deletions
src/ast/ast.rs
src/ast/main.rs
+27
-0
27 additions, 0 deletions
src/ast/main.rs
src/ast/parser.lalrpop
+2
-2
2 additions, 2 deletions
src/ast/parser.lalrpop
with
41 additions
and
4 deletions
src/ast/ast.rs
+
12
−
2
View file @
b8afed80
...
@@ -2,11 +2,13 @@ use std::fmt;
...
@@ -2,11 +2,13 @@ use std::fmt;
// ast
// ast
pub
type
Id
=
String
;
// println!("{:?}", ..)
// println!("{:?}", ..)
#[derive(Debug)]
#[derive(Debug)]
pub
enum
NumOrId
{
pub
enum
NumOrId
{
Num
(
usize
),
Num
(
i32
),
Id
(
String
),
Id
(
Id
),
}
}
// println!("{}", ..)
// println!("{}", ..)
...
@@ -19,3 +21,11 @@ impl fmt::Display for NumOrId {
...
@@ -19,3 +21,11 @@ impl fmt::Display for NumOrId {
Ok
(())
Ok
(())
}
}
}
}
pub
type
Stmts
=
Vec
<
Stmt
>
;
#[derive(Debug)]
pub
enum
Stmt
{
Let
(
Id
,
NumOrId
),
If
(
Id
,
Stmts
,
Option
<
Stmts
>
),
}
This diff is collapsed.
Click to expand it.
src/ast/main.rs
+
27
−
0
View file @
b8afed80
...
@@ -5,6 +5,7 @@ lalrpop_mod!(pub parser, "/ast/parser.rs");
...
@@ -5,6 +5,7 @@ lalrpop_mod!(pub parser, "/ast/parser.rs");
use
parser
::
*
;
use
parser
::
*
;
pub
mod
ast
;
pub
mod
ast
;
use
ast
::
*
;
fn
main
()
{
fn
main
()
{
println!
(
"minimal"
);
println!
(
"minimal"
);
...
@@ -26,3 +27,29 @@ fn parse_num_or_id() {
...
@@ -26,3 +27,29 @@ fn parse_num_or_id() {
"a1_a"
"a1_a"
);
);
}
}
type
Error
=
String
;
fn
type_check
(
stmts
:
&
Stmts
)
->
Result
<
(),
Error
>
{
for
i
in
stmts
{
match
i
{
Stmt
::
Let
(
_
,
_
)
=>
{
println!
(
"let"
);
}
Stmt
::
If
(
_
,
_
,
__
)
=>
{
println!
(
"if"
);
}
}
}
Ok
(())
}
#[test]
fn
test_stmts
()
{
let
stmts
=
vec!
[
Stmt
::
Let
(
"a"
.to_string
(),
NumOrId
::
Num
(
1
)),
Stmt
::
Let
(
"b"
.to_string
(),
NumOrId
::
Num
(
2
)),
Stmt
::
Let
(
"c"
.to_string
(),
NumOrId
::
Num
(
3
)),
];
type_check
(
&
stmts
)
.unwrap
();
}
This diff is collapsed.
Click to expand it.
src/ast/parser.lalrpop
+
2
−
2
View file @
b8afed80
...
@@ -9,8 +9,8 @@ pub NumOrId: NumOrId = {
...
@@ -9,8 +9,8 @@ pub NumOrId: NumOrId = {
Id => NumOrId::Id(<>),
Id => NumOrId::Id(<>),
}
}
pub Num:
usize
= {
pub Num:
i32
= {
r"[0-9]+" =>
usize
::from_str(<>).unwrap(),
r"[0-9]+" =>
i32
::from_str(<>).unwrap(),
};
};
pub Id: String = {
pub Id: String = {
...
...
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