This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Conditional statement #148
Open
remaininlight
wants to merge
4
commits into
monadgroup:master
Choose a base branch
from
remaininlight:dev
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1910cf3
If function, not switching value on condition change
remaininlight 692178f
If function working with Any type. Type safety bypassed for Any type..
remaininlight 05780ce
Merge branch 'master' into dev
remaininlight 49bc440
Updated number of functions in mir block after merge from master
remaininlight File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use super::{Function, FunctionContext, VarArgs}; | ||
use codegen::values::{AnyValue, NumValue}; | ||
use inkwell::values::PointerValue; | ||
use inkwell::{FloatPredicate}; | ||
use mir::block; | ||
|
||
pub struct IfFunction {} | ||
impl Function for IfFunction { | ||
fn function_type() -> block::Function { block::Function::If } | ||
|
||
fn gen_call( | ||
func: &mut FunctionContext, | ||
args: &[PointerValue], | ||
_varargs: Option<VarArgs>, | ||
result: PointerValue, | ||
) { | ||
|
||
let condition_num = NumValue::new(args[0]); | ||
let condition_vec = condition_num.get_vec(func.ctx.b); | ||
let left_index = func.ctx.context.i32_type().const_int(0, false); | ||
let condition_left = func.ctx | ||
.b | ||
.build_extract_element(&condition_vec, &left_index, "condition.left") | ||
.into_float_value(); | ||
let condition_bool = func.ctx.b.build_float_compare( | ||
FloatPredicate::ONE, | ||
condition_left, | ||
func.ctx.context.f32_type().const_float(0.0), | ||
"conditionbool", | ||
); | ||
|
||
let then_any = AnyValue::new(args[1]); | ||
let alternative_any = AnyValue::new(args[2]); | ||
let left_index = func.ctx.context.i32_type().const_int(0, false); | ||
let evaluated = func.ctx.b.build_select(condition_bool, | ||
then_any.get_value_ptr(), | ||
alternative_any.get_value_ptr(), | ||
"evaluated"); | ||
let evaluated_ptr = evaluated.as_pointer_value(); | ||
let evaluated_any = AnyValue::new(*evaluated_ptr); | ||
let result_any = AnyValue::new(result); | ||
|
||
evaluated_any.copy_to(func.ctx.b, func.ctx.module, &result_any); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use codegen::util; | ||
use inkwell::builder::Builder; | ||
use inkwell::context::Context; | ||
use inkwell::module::Module; | ||
use inkwell::types::StructType; | ||
use inkwell::values::{IntValue, PointerValue}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct AnyValue { | ||
pub val: PointerValue, | ||
} | ||
|
||
impl AnyValue { | ||
pub fn get_type(context: &Context) -> StructType { | ||
// TODO Just returning Number structure for now. Can this infer what its correct type should be from context? | ||
// Maybe return space to hold largest possible type? | ||
context.struct_type( | ||
&[&context.f32_type().vec_type(2), &context.i8_type()], | ||
false, | ||
) | ||
} | ||
|
||
pub fn new(val: PointerValue) -> Self { | ||
AnyValue { val } | ||
} | ||
|
||
pub fn copy_to(&self, builder: &mut Builder, module: &Module, other: &AnyValue) { | ||
util::copy_ptr(builder, module, self.val, other.val) | ||
} | ||
|
||
pub fn get_value_ptr(&self) -> PointerValue { | ||
self.val | ||
} | ||
|
||
pub fn get_form_ptr(&self, builder: &mut Builder) -> PointerValue { | ||
unsafe { builder.build_struct_gep(&self.val, 1, "any.form.ptr") } | ||
} | ||
|
||
pub fn get_form(&self, builder: &mut Builder) -> IntValue { | ||
let vec = self.get_form_ptr(builder); | ||
builder.build_load(&vec, "any.form").into_int_value() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,9 @@ use {CompileError, CompileResult}; | |
// lowers an AST into a Block MIR object | ||
pub fn lower_ast(id: mir::BlockId, block: &ast::Block) -> CompileResult<mir::Block> { | ||
let mut lower = AstLower::new(id); | ||
println!("--- Expressions in block before lowering AST:"); | ||
for expr in &block.expressions { | ||
println!("{:?}", expr); | ||
lower.lower_expression(expr)?; | ||
} | ||
Ok(lower.block) | ||
|
@@ -769,7 +771,9 @@ impl<'a> AstLower<'a> { | |
expected_type: mir::VarType, | ||
actual_type: mir::VarType, | ||
) -> Option<CompileError> { | ||
if expected_type != actual_type { | ||
if (expected_type != actual_type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type safety checking is disabled for Any type here. It would be good if there was some way of linking the output type of a function etc to its input types? ie. an if function has midi messages as its arguments, so the output takes the type of a midi message, haven't worked out the best way to do this yet. |
||
&& (expected_type != mir::VarType::Any) | ||
&& (actual_type != mir::VarType::Any) { | ||
Some(CompileError::mismatched_type( | ||
expected_type, | ||
actual_type, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what should be returned here, think some restructuring might be necessary in order to infer the actual type the AnyValue is representing and return space for that?