Skip to content

Commit

Permalink
Else block!
Browse files Browse the repository at this point in the history
  • Loading branch information
RedstoneWizard08 committed Mar 11, 2024
1 parent dd4f507 commit e68ee59
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions crates/qsc-ast/src/ast/stmt/cond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pub struct ConditionalNode {
pub span: StaticSpan,
pub condition: Node,
pub block: Block,
pub else_block: Option<Block>,
}
19 changes: 14 additions & 5 deletions crates/qsc-codegen/src/generator/cond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ pub trait ConditionalCompiler<'a, 'b, M: Module>: Backend<'a, 'b, M> {
fn compile_conditional(
cctx: &RwLock<CompilerContext<M>>,
ctx: &mut CodegenContext<'a, 'b>,
expr: ConditionalNode,
cond: ConditionalNode,
) -> Result<Value>;
}

impl<'a, 'b, M: Module, T: Backend<'a, 'b, M>> ConditionalCompiler<'a, 'b, M> for T {
fn compile_conditional(
cctx: &RwLock<CompilerContext<M>>,
ctx: &mut CodegenContext<'a, 'b>,
expr: ConditionalNode,
cond: ConditionalNode,
) -> Result<Value> {
let ptr = Self::ptr(cctx);
let cond_value = Self::compile(cctx, ctx, expr.condition)?;
let cond_value = Self::compile(cctx, ctx, cond.condition)?;
let mut builder = ctx.builder.write();

let then = builder.create_block();
Expand All @@ -40,7 +40,7 @@ impl<'a, 'b, M: Module, T: Backend<'a, 'b, M>> ConditionalCompiler<'a, 'b, M> fo

RwLockWriteGuard::unlock_fair(builder);

for node in expr.block.data {
for node in cond.block.data {
// TODO: Use last value as a return
Self::compile(cctx, ctx, node)?;
}
Expand All @@ -54,7 +54,16 @@ impl<'a, 'b, M: Module, T: Backend<'a, 'b, M>> ConditionalCompiler<'a, 'b, M> fo

let else_ret = builder.ins().iconst(ptr, 0);

// TODO: Else conditions
RwLockWriteGuard::unlock_fair(builder);

if let Some(else_block) = cond.else_block {
for node in else_block.data {
// TODO: Use last value as a return
Self::compile(cctx, ctx, node)?;
}
}

let mut builder = ctx.builder.write();

builder.ins().jump(merge, &[else_ret]);

Expand Down
7 changes: 7 additions & 0 deletions crates/qsc-lexer/src/cond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ impl<'i> Lexer {
let condition = self.parse(inner.next().unwrap())?;
let block = self.parse_data(inner.next().unwrap())?.as_block()?;

let mut else_block = None;

if let Some(val) = inner.next() {
else_block = Some(self.parse_data(val)?.as_block()?);
}

Ok(ConditionalNode {
span: pair.as_span().into(),
condition,
block,
else_block,
})
}
}
2 changes: 1 addition & 1 deletion crates/qsc-lexer/src/quickscript.pest
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ expr = { binary_op | term }
term = { call | literal | ident }
statement = { real_stmt ~ ";"? | COMMENT | WHITESPACE }
real_stmt = { conditional | ret | var | expr | block }
conditional = { "if" ~ expr ~ block }
conditional = { "if" ~ expr ~ block ~ ("else" ~ block)? }

// Keywords
var = { "let" ~ "mut"? ~ ident ~ (":" ~ type)? ~ "=" ~ expr ~ ";" }
Expand Down
4 changes: 3 additions & 1 deletion dev/main.qs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ fn main() -> i32 {
let b = 2;
let val = do_math(a, b);

if val == 2 {
if val == 3 {
printf("It works!\n");

return 0;
} else {
printf("It wasn't 3...\n");
}

printf("Hello, %s!\n", get_name());
Expand Down

0 comments on commit e68ee59

Please sign in to comment.