Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
D7050E
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
Model registry
Operate
Environments
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
Gustav Hansson
D7050E
Commits
134b920c
Commit
134b920c
authored
5 years ago
by
Per
Browse files
Options
Downloads
Patches
Plain Diff
return command
parent
081c1e30
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/crust.rs
+103
-29
103 additions, 29 deletions
examples/crust.rs
with
103 additions
and
29 deletions
examples/crust.rs
+
103
−
29
View file @
134b920c
use
crust
::{
use
crust
::{
ast
::{
Expr
,
Func
,
Op
,
Span
,
SpanExpr
},
ast
::{
Block
,
Cmd
,
Expr
,
Func
,
Op
,
Span
,
SpanExpr
},
parse
::{
parse_block
,
parse_expr
},
parse
::{
parse_block
,
parse_expr
},
};
};
...
@@ -11,7 +11,8 @@ use inkwell::{
...
@@ -11,7 +11,8 @@ use inkwell::{
passes
::
PassManager
,
passes
::
PassManager
,
types
::
BasicTypeEnum
,
types
::
BasicTypeEnum
,
values
::{
values
::{
BasicValueEnum
,
FloatValue
,
FunctionValue
,
IntValue
,
PointerValue
,
BasicValueEnum
,
FloatValue
,
FunctionValue
,
InstructionValue
,
IntValue
,
PointerValue
,
},
},
FloatPredicate
,
OptimizationLevel
,
FloatPredicate
,
OptimizationLevel
,
};
};
...
@@ -29,16 +30,15 @@ fn main() -> Result<(), Box<dyn Error>> {
...
@@ -29,16 +30,15 @@ fn main() -> Result<(), Box<dyn Error>> {
let
execution_engine
=
let
execution_engine
=
module
.create_jit_execution_engine
(
OptimizationLevel
::
None
)
?
;
module
.create_jit_execution_engine
(
OptimizationLevel
::
None
)
?
;
// match parse_block(Span::new(
match
parse_block
(
Span
::
new
(
// "
"
// {
{
// return = 2 + 3;
return 2 + 3
// }
}
// ",
"
,
// ))
))
{
match
parse_expr
(
Span
::
new
(
"2 + 3"
))
{
Ok
((
_
,
prog
))
=>
{
Ok
((
_
,
expr
))
=>
{
println!
(
"{:?}"
,
&
prog
);
println!
(
"{:?}"
,
&
expr
);
let
u32_type
=
context
.i32_type
();
let
u32_type
=
context
.i32_type
();
let
fn_type
=
u32_type
.fn_type
(
&
[],
false
);
let
fn_type
=
u32_type
.fn_type
(
&
[],
false
);
let
function
=
module
.add_function
(
"expr"
,
fn_type
,
None
);
let
function
=
module
.add_function
(
"expr"
,
fn_type
,
None
);
...
@@ -49,10 +49,11 @@ fn main() -> Result<(), Box<dyn Error>> {
...
@@ -49,10 +49,11 @@ fn main() -> Result<(), Box<dyn Error>> {
context
:
&
context
,
context
:
&
context
,
builder
:
&
builder
,
builder
:
&
builder
,
module
:
&
module
,
module
:
&
module
,
fn_value_opt
:
Some
(
function
),
//&fpm,
//&fpm,
};
};
let
res
=
compiler
.compile_
expr
(
&
expr
);
let
res
=
compiler
.compile_
block
(
prog
);
builder
.build_return
(
Some
(
&
res
));
//
builder.build_return(Some(&res));
let
fun_expr
:
JitFunction
<
ExprFunc
>
=
let
fun_expr
:
JitFunction
<
ExprFunc
>
=
unsafe
{
execution_engine
.get_function
(
"expr"
)
.ok
()
.unwrap
()
};
unsafe
{
execution_engine
.get_function
(
"expr"
)
.ok
()
.unwrap
()
};
...
@@ -75,10 +76,22 @@ pub struct Compiler<'a> {
...
@@ -75,10 +76,22 @@ pub struct Compiler<'a> {
// pub function: &'a Func<'a>,
// pub function: &'a Func<'a>,
// variables: HashMap<String, PointerValue>,
// variables: HashMap<String, PointerValue>,
//
fn_value_opt: Option<FunctionValue>,
fn_value_opt
:
Option
<
FunctionValue
>
,
}
}
impl
<
'a
>
Compiler
<
'a
>
{
impl
<
'a
>
Compiler
<
'a
>
{
/// Gets a defined function given its name.
#[inline]
fn
get_function
(
&
self
,
name
:
&
str
)
->
Option
<
FunctionValue
>
{
self
.module
.get_function
(
name
)
}
/// Returns the `FunctionValue` representing the function being compiled.
#[inline]
fn
fn_value
(
&
self
)
->
FunctionValue
{
self
.fn_value_opt
.unwrap
()
}
fn
compile_expr
(
&
self
,
expr
:
&
SpanExpr
)
->
IntValue
{
fn
compile_expr
(
&
self
,
expr
:
&
SpanExpr
)
->
IntValue
{
match
expr
.1
.clone
()
{
match
expr
.1
.clone
()
{
Expr
::
Num
(
i
)
=>
self
.context
.i32_type
()
.const_int
(
i
as
u64
,
false
),
Expr
::
Num
(
i
)
=>
self
.context
.i32_type
()
.const_int
(
i
as
u64
,
false
),
...
@@ -87,26 +100,87 @@ impl<'a> Compiler<'a> {
...
@@ -87,26 +100,87 @@ impl<'a> Compiler<'a> {
let
lv
=
self
.compile_expr
(
&
l
);
let
lv
=
self
.compile_expr
(
&
l
);
let
rv
=
self
.compile_expr
(
&
r
);
let
rv
=
self
.compile_expr
(
&
r
);
match
op
{
match
op
{
Op
::
Add
=>
self
.builder
.build_int_add
(
lv
,
rv
,
"sum"
)
Op
::
Add
=>
self
.builder
.build_int_add
(
lv
,
rv
,
"sum"
),
,
// Op::Sub => lv - rv,
// Op::Mul => lv * rv,
// Op::Div => lv / rv,
// Op::Pow => lv.pow(rv as u32),
_
=>
unimplemented!
(),
_
=>
unimplemented!
(),
}
}
}
}
// Expr::UnaryOp(op, e) => {
_
=>
unimplemented!
(),
// let e = eval_expr(&e);
}
// match op {
}
// Op::Add => e,
// Op::Sub => -e,
/// Creates a new stack allocation instruction in the entry block of the function.
// _ => unimplemented!(),
fn
create_entry_block_alloca
(
&
self
,
name
:
&
str
)
->
PointerValue
{
let
builder
=
self
.context
.create_builder
();
let
entry
=
self
.fn_value
()
.get_first_basic_block
()
.unwrap
();
match
entry
.get_first_instruction
()
{
Some
(
first_instr
)
=>
builder
.position_before
(
&
first_instr
),
None
=>
builder
.position_at_end
(
&
entry
),
}
builder
.build_alloca
(
self
.context
.f64_type
(),
name
)
}
fn
compile_cmd
(
&
self
,
cmd
:
&
Cmd
)
->
(
InstructionValue
,
bool
)
{
println!
(
"{:?}"
,
cmd
);
match
cmd
{
// Cmd::Assign(lexp, rexp) => {
// let rval = eval_expr(rexp, mem, venv, fenv);
// println!("val {:?}", rval);
// let addr = eval_lvalue(lexp, mem, venv, fenv);
// // println!("lval {:?}", lval);
// // let addr = venv.get(&lval).unwrap();
// mem.Menv.insert(addr, rval);
// None
// }
// Cmd::If(exp, then_block, opt_else) => {
// if get_bool(eval_expr(exp, mem, venv, fenv)) {
// eval_body(then_block.to_vec(), mem, venv, fenv)
// } else {
// if let Some(else_block) = opt_else {
// eval_body(else_block.to_vec(), mem, venv, fenv)
// } else {
// None
// }
// }
// }
// }
// Cmd::Let(_, id, _, exp) => {
// let val = eval_expr(exp, stack, fenv);
// println!("val {:?}", val);
// // let addr = mem.alloc(); // get new allocation slot
// // mem.Menv.insert(addr, val); // write the new value
// // venv.insert(id.to_owned(), addr);
// // dump("after Let", mem, venv);
// None
// }
Cmd
::
Return
(
exp
)
=>
{
let
expr
=
self
.compile_expr
(
exp
);
(
self
.builder
.build_return
(
Some
(
&
expr
)),
true
)
}
// Cmd::While(exp, body) => {
// while get_bool(eval_expr(exp, mem, venv, fenv)) {
// if let Some(retv) = eval_body(body.to_vec(), mem, venv, fenv) {
// return Some(retv);
// }
// }
// None
// }
// }
_
=>
unimplemented!
(),
_
=>
unimplemented!
(),
}
}
}
}
pub
fn
compile_block
(
&
self
,
cmds
:
Vec
<
Cmd
>
)
->
InstructionValue
{
for
c
in
&
cmds
{
let
(
cmd
,
ret
)
=
self
.compile_cmd
(
c
);
if
ret
{
return
cmd
;
}
}
panic!
();
}
}
}
// fn main() {
// fn main() {
...
...
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