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
a382754b
Commit
a382754b
authored
Sep 17, 2020
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
passes w1_2
parent
767f2612
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/w1_2.rs
+4
-5
4 additions, 5 deletions
examples/w1_2.rs
src/check.rs
+12
-9
12 additions, 9 deletions
src/check.rs
src/grammar.lalrpop
+2
-1
2 additions, 1 deletion
src/grammar.lalrpop
with
18 additions
and
15 deletions
examples/w1_2.rs
+
4
−
5
View file @
a382754b
...
...
@@ -16,12 +16,11 @@ fn c(x: i32, y: i32) -> i32 {
-
a
-
(
-
b
)
*
y
// here we have prefix operator "-"
}
// More advanced statements
// a function taking two bool arguments returning the bool type
// with some let statements and function calls
fn
a
(
x
:
bool
,
y
:
bool
)
->
bool
{
fn
a
1
(
x
:
bool
,
y
:
bool
)
->
bool
{
if
x
&&
y
{
let
a
:
bool
=
true
;
y
||
a
...
...
@@ -32,8 +31,8 @@ fn a(x: bool, y: bool) -> bool {
// 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
);
fn
b
1
(
x
:
bool
,
y
:
bool
)
->
i32
{
let
a
:
bool
=
a
1
(
x
,
y
||
false
);
let
mut
b
:
i32
=
0
;
if
a
&&
y
{
let
a
:
bool
=
true
;
// shadowing
...
...
@@ -50,7 +49,7 @@ fn b(x: bool, y: bool) -> i32 {
// a function taking two bool arguments returning the i32 type
// while
fn
c
(
x
:
bool
,
y
:
bool
)
->
i32
{
fn
c
1
(
x
:
bool
,
y
:
bool
)
->
i32
{
let
mut
b
:
i32
=
0
;
let
mut
c
:
i32
=
1
;
while
(
b
<
10
)
{
...
...
This diff is collapsed.
Click to expand it.
src/check.rs
+
12
−
9
View file @
a382754b
...
...
@@ -45,6 +45,8 @@ fn expr_type(
)
->
Result
<
Type
,
Error
>
{
use
Expr
::
*
;
trace!
(
"expr_type {}"
,
e
);
trace!
(
"var_env {:?}"
,
var_env
);
match
e
{
Num
(
_
)
=>
Ok
(
Type
::
I32
),
Bool
(
_
)
=>
Ok
(
Type
::
Bool
),
...
...
@@ -57,15 +59,16 @@ fn expr_type(
use
Op
::
*
;
match
op
{
// Arithmetic and Boole
a
n
// Arithmetic and Boolen
Add
|
Mul
|
Div
|
Sub
|
And
|
Or
=>
{
// check if op and args are of i32
if
lt
==
Type
::
I32
&&
rt
==
Type
::
I32
{
Ok
(
Type
::
I32
)
// check if op and args are compliant
let
opt
=
op
.get_type
();
if
lt
==
opt
&&
rt
==
opt
{
Ok
(
opt
)
}
else
{
Err
(
format!
(
"
Num operation requires i32, left {} right {
}"
,
l
t
,
rt
"
Expected type {}, found, left {}: {}, {:?} right {}: {}, {:?
}"
,
opt
,
l
,
lt
,
lt
,
r
,
r
t
,
rt
))
}
}
...
...
@@ -373,7 +376,7 @@ pub fn check(p: &Program) -> Result<(), Error> {
trace!
(
"Input program
\n
{}"
,
&
p
);
for
fd
in
p
.fn_decls
.iter
()
{
trace!
(
"function
id
{}"
,
fd
.id
);
trace!
(
"
check
function
\n
{}"
,
fd
);
let
mut
var_env
=
VarEnv
::
new
();
// build a scope for the arguments
...
...
@@ -390,9 +393,9 @@ pub fn check(p: &Program) -> Result<(), Error> {
var_env
.push_param_scope
(
arg_ty
);
let
stmt_type
=
check_stmts
(
&
fd
.body
,
&
fn_env
,
&
type_env
,
&
mut
var_env
);
trace!
(
"
function id
{}: {:?}"
,
fd
.id
,
&
stmt_type
);
trace!
(
"
result
{}: {:?}"
,
fd
.id
,
&
stmt_type
);
let
stmt_type
=
stmt_type
?
;
let
stmt_type
=
strip_mut
(
stmt_type
?
)
;
if
stmt_type
!=
fd
.result
{
Err
(
format!
(
...
...
This diff is collapsed.
Click to expand it.
src/grammar.lalrpop
+
2
−
1
View file @
a382754b
...
...
@@ -150,9 +150,10 @@ Stmt : Stmt = {
Type : Type = {
Id
=> Type::
Named(<>)
,
"bool"
=> Type::
Bool
,
"()" => Type::Unit,
"i32" => Type::I32, // this should likely be a paratrized Num type later
Id => Type::Named(<>),
"&" <Type> => Type::Ref(Box::new(<>)),
"&" "mut" <Type> => Type::Ref(Box::new(Type::Mut(Box::new(<>)))),
}
...
...
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