Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions example/match.flint
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
pub fn main() Int {
mut x: Int = 10
x = 1
x
@external(c, "flint_stdlib", "assert")
pub fn assert(cond: Bool) Nil

@external(c, "flint_stdlib", "print")
pub fn print(string: String) Nil

pub fn println(v: String) Nil {
print(v)
print("\n")
}

pub fn main() Nil {
mut x: String = "Hi Universe!"
println(x)
x = "Hello World!"
println(x)
}
32 changes: 32 additions & 0 deletions internal/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,38 @@ func (cg *CodeGen) emitCall(b *ir.Block, c *parser.CallExpr, isTail bool) value.
return callInst
}

func (cg *CodeGen) emitAssign(b *ir.Block, e *parser.AssignExpr) value.Value {
expr := cg.emitExpr(b, e.Value, false)
alloc := cg.locals[e.Name.Name]
b.NewStore(expr, alloc)
return alloc
}

func (cg *CodeGen) emitVarDecl(b *ir.Block, e *parser.VarDeclExpr) value.Value {
expr := cg.emitExpr(b, e.Value, false)
alloc := b.NewAlloca(expr.Type())
cg.locals[e.Name.Lexeme] = alloc
b.NewStore(expr, alloc)
return alloc
}

func (cg *CodeGen) emitPrefix(b *ir.Block, e *parser.PrefixExpr) value.Value {
expr := cg.emitExpr(b, e.Right, false)
ty := expr.Type()

switch e.Operator.Kind {
case lexer.Minus:
intType := ty.(*types.IntType)
return b.NewSub(constant.NewInt(intType, 0), expr)
case lexer.MinusDot:
floatType := ty.(*types.FloatType)
return b.NewFSub(constant.NewFloat(floatType, 0), expr)
case lexer.Bang:
return b.NewXor(constant.True, expr)
}
return nil
}

func (cg *CodeGen) emitDefaultReturn(b *ir.Block, ret types.Type, isMain bool) {
if isMain {
b.NewRet(constant.NewInt(types.I32, 0))
Expand Down
6 changes: 6 additions & 0 deletions internal/codegen/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func (cg *CodeGen) emitExpr(b *ir.Block, e parser.Expr, isTail bool) value.Value
return cg.emitIf(b, v)
case *parser.MatchExpr:
return cg.emitMatch(b, v, isTail)
case *parser.VarDeclExpr:
return cg.emitVarDecl(b, v)
case *parser.AssignExpr:
return cg.emitAssign(b, v)
case *parser.PrefixExpr:
return cg.emitPrefix(b, v)
default:
panic("unsupported expression type")
}
Expand Down
2 changes: 2 additions & 0 deletions internal/codegen/infix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func (cg *CodeGen) emitInfix(b *ir.Block, e *parser.InfixExpr) value.Value {
l := cg.emitExpr(b, e.Left, false)
r := cg.emitExpr(b, e.Right, false)

switch e.Operator.Kind {
case lexer.Plus:
return b.NewAdd(l, r)
Expand Down Expand Up @@ -52,6 +53,7 @@ func (cg *CodeGen) emitInfix(b *ir.Block, e *parser.InfixExpr) value.Value {
case lexer.GreaterEqualDot:
return b.NewFCmp(enum.FPredOGE, l, r)
case lexer.LtGt:
// return cg.emitConcat(b, e, l, r)
return nil
case lexer.AmperAmper:
return b.NewAnd(l, r)
Expand Down