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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Per Lindgren
erode
Commits
54e2b2b1
Commit
54e2b2b1
authored
4 years ago
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
prepare for the Unknown
parent
83553bc9
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
src/ast.rs
+1
-0
1 addition, 0 deletions
src/ast.rs
src/check.rs
+35
-30
35 additions, 30 deletions
src/check.rs
src/env.rs
+2
-2
2 additions, 2 deletions
src/env.rs
with
38 additions
and
32 deletions
src/ast.rs
+
1
−
0
View file @
54e2b2b1
...
...
@@ -76,6 +76,7 @@ pub enum Type {
I32
,
// i32
Ref
(
Box
<
Type
>
),
// & mut? Type -- used in parser
Mut
(
Box
<
Type
>
),
// mut Type
Unknown
,
// Not yet assigned
// RefMut(Box<Type>), // &mut Type -- used in parser
}
...
...
This diff is collapsed.
Click to expand it.
src/check.rs
+
35
−
30
View file @
54e2b2b1
...
...
@@ -44,6 +44,7 @@ fn expr_type(
var_env
:
&
VarEnv
,
)
->
Result
<
Type
,
Error
>
{
use
Expr
::
*
;
trace!
(
"expr_type {}"
,
e
);
match
e
{
Num
(
_
)
=>
Ok
(
Type
::
I32
),
Bool
(
_
)
=>
Ok
(
Type
::
Bool
),
...
...
@@ -134,38 +135,38 @@ fn expr_type(
// ))
// }
// }
// Id(id) => match var_env.get(id.to_string()) {
// Some(t) => match t {
// (is_mut, Some(t)) => Ok((*is_mut, t.clone())),
// (_, None) => Err(format!("variable {:?} has no type yet", id)),
// },
// None => Err(format!("variable not found {}", id)),
// },
Id
(
id
)
=>
match
var_env
.get
(
id
.to_string
())
{
Some
(
t
)
=>
match
t
{
// variable bound to type
Some
(
t
)
=>
Ok
(
t
.clone
()),
// not yet bound
None
=>
Err
(
format!
(
"variable {:?} has no type yet"
,
id
)),
},
None
=>
Err
(
format!
(
"variable not found {}"
,
id
)),
},
As
(
_e
,
_t
)
=>
unimplemented!
(
"here we implement explicit type cast"
),
// for now, allow only references to variables
// should we make recursive call here?
// Ref(ref_e) => {
// let (_, t) = expr_type(ref_e, fn_env, type_env, var_env)?;
// Ok(Type::Ref(Box::new((false, t)))
// }
// Convert Expr::Ref to Type::Ref
Ref
(
ref_e
)
=>
{
let
t
=
expr_type
(
ref_e
,
fn_env
,
type_env
,
var_env
)
?
;
Ok
(
Type
::
Ref
(
Box
::
new
(
t
)))
}
// // for now, allow only references to variables
// // should we make recursive call here?
// RefMut(ref_mut_e) => {
// let t = expr_type(ref_mut_e, fn_env, type_env, var_env)?;
// Ok(Type::RefMut(Box::new(t)))
// }
// Convert Expr::Mut to Type::Mut
RefMut
(
ref_mut_e
)
=>
{
let
t
=
expr_type
(
ref_mut_e
,
fn_env
,
type_env
,
var_env
)
?
;
Ok
(
Type
::
Mut
(
Box
::
new
(
t
)))
}
// DeRef(deref_e) => {
// let t = expr_type(deref_e, fn_env, type_env, var_env)?;
// trace!("deref_t {}", &t);
// match t {
// Type::Ref(dt) => Ok(*dt),
// Type::RefMut(dt) => Ok(*dt),
// _ => Err(format!("cannot deref {}", e)),
// }
// }
DeRef
(
deref_e
)
=>
{
let
t
=
expr_type
(
deref_e
,
fn_env
,
type_env
,
var_env
)
?
;
trace!
(
"deref_t {}"
,
&
t
);
match
t
{
Type
::
Ref
(
dt
)
=>
Ok
(
*
dt
),
_
=>
Err
(
format!
(
"cannot deref {} of type {}"
,
e
,
t
)),
}
}
_
=>
unimplemented!
(),
}
}
...
...
@@ -342,8 +343,12 @@ pub fn check_stmts(
(
None
,
Some
(
e
))
=>
Some
(
expr_type
(
&*
e
,
fn_env
,
type_env
,
var_env
)
?
),
(
ot
,
None
)
=>
ot
.clone
(),
};
// var_env.new_id(var_id.clone(), *is_mut, ot);
// trace!("var_env {:?}", var_env);
let
ot
=
match
is_mut
{
true
=>
Type
::
Mut
(
Box
::
new
(
ot
)),
false
=>
ot
,
};
var_env
.new_id
(
var_id
.clone
(),
ot
);
trace!
(
"var_env {:?}"
,
var_env
);
Ok
(
Type
::
Unit
)
}
...
...
This diff is collapsed.
Click to expand it.
src/env.rs
+
2
−
2
View file @
54e2b2b1
...
...
@@ -22,8 +22,8 @@ pub fn new_fn_env(v: &Vec<FnDecl>) -> FnEnv {
HashMap
::
from_iter
(
i
)
}
//
(bool,
Option<Type>
)
//
variable is_mut,
// Option<Type>
//
// Some<Type>, variable has a declared type
// None, variable has been introduced, but type not yet determined
...
...
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