Skip to content

Commit

Permalink
language: introduce the requirement to use return to return a value f…
Browse files Browse the repository at this point in the history
…rom a function
  • Loading branch information
azenla committed Nov 21, 2023
1 parent 5540918 commit 0a2d029
Show file tree
Hide file tree
Showing 27 changed files with 115 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A work-in-progress programming language.
```pork
/* fibonacci sequence */
func fib(n) {
if n < 2 {
return if n < 2 {
n
} else {
fib(n - 1) + fib(n - 2)
Expand Down
5 changes: 5 additions & 0 deletions ast/src/main/ast/pork.yml
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ types:
Continue:
parent: Expression
values: []
Return:
parent: Expression
values:
- name: value
type: Expression
NoneLiteral:
parent: Expression
values: []
Expand Down
3 changes: 3 additions & 0 deletions ast/src/main/graph/types.dot
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ digraph A {
type_ForIn [shape=box,label="ForIn"]
type_Break [shape=box,label="Break"]
type_Continue [shape=box,label="Continue"]
type_Return [shape=box,label="Return"]
type_NoneLiteral [shape=box,label="NoneLiteral"]
type_NativeFunctionDescriptor [shape=box,label="NativeFunctionDescriptor"]
type_IndexedBy [shape=box,label="IndexedBy"]
Expand Down Expand Up @@ -69,6 +70,7 @@ digraph A {
type_Expression -> type_ForIn
type_Expression -> type_Break
type_Expression -> type_Continue
type_Expression -> type_Return
type_Expression -> type_NoneLiteral
type_Expression -> type_IndexedBy
type_Definition -> type_FunctionDefinition
Expand Down Expand Up @@ -116,6 +118,7 @@ digraph A {
type_ForIn -> type_ForInItem [style=dotted]
type_ForIn -> type_Expression [style=dotted]
type_ForIn -> type_Block [style=dotted]
type_Return -> type_Expression [style=dotted]
type_NativeFunctionDescriptor -> type_Symbol [style=dotted]
type_NativeFunctionDescriptor -> type_StringLiteral [style=dotted]
type_IndexedBy -> type_Expression [style=dotted]
Expand Down
3 changes: 3 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/gen/NodeCoalescer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ class NodeCoalescer(val followChildren: Boolean = true, val handler: (Node) -> U
override fun visitPrefixOperation(node: PrefixOperation): Unit =
handle(node)

override fun visitReturn(node: Return): Unit =
handle(node)

override fun visitSetAssignment(node: SetAssignment): Unit =
handle(node)

Expand Down
2 changes: 2 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/gen/NodeParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ interface NodeParser {

fun parsePrefixOperation(): PrefixOperation

fun parseReturn(): Return

fun parseSetAssignment(): SetAssignment

fun parseStringLiteral(): StringLiteral
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fun NodeParser.parse(type: NodeType): Node =
NodeType.ForIn -> parseForIn()
NodeType.Break -> parseBreak()
NodeType.Continue -> parseContinue()
NodeType.Return -> parseReturn()
NodeType.NoneLiteral -> parseNoneLiteral()
NodeType.NativeFunctionDescriptor -> parseNativeFunctionDescriptor()
NodeType.IndexedBy -> parseIndexedBy()
Expand Down
1 change: 1 addition & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/gen/NodeType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum class NodeType(val parent: NodeType? = null) {
NoneLiteral(Expression),
Parentheses(Expression),
PrefixOperation(Expression),
Return(Expression),
SetAssignment(Expression),
StringLiteral(Expression),
SuffixOperation(Expression),
Expand Down
2 changes: 2 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/gen/NodeVisitor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ interface NodeVisitor<T> {

fun visitPrefixOperation(node: PrefixOperation): T

fun visitReturn(node: Return): T

fun visitSetAssignment(node: SetAssignment): T

fun visitStringLiteral(node: StringLiteral): T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
is ForIn -> visitForIn(node)
is Break -> visitBreak(node)
is Continue -> visitContinue(node)
is Return -> visitReturn(node)
is NoneLiteral -> visitNoneLiteral(node)
is NativeFunctionDescriptor -> visitNativeFunctionDescriptor(node)
is IndexedBy -> visitIndexedBy(node)
Expand Down
28 changes: 28 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/gen/Return.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast.gen

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
@SerialName("return")
class Return(val value: Expression) : Expression() {
override val type: NodeType = NodeType.Return

override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(value)

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitReturn(this)

override fun equals(other: Any?): Boolean {
if (other !is Return) return false
return other.value == value
}

override fun hashCode(): Int {
var result = value.hashCode()
result = 31 * result + type.hashCode()
return result
}
}
10 changes: 9 additions & 1 deletion bytecode/src/main/kotlin/gay/pizza/pork/bytecode/Op.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ package gay.pizza.pork.bytecode
import kotlinx.serialization.Serializable

@Serializable
data class Op(val code: Opcode, val args: List<UInt>)
class Op(val code: Opcode, val args: List<UInt>) {
override fun toString(): String = buildString {
append(code.name)
if (args.isNotEmpty()) {
append(" ")
append(args.joinToString(" "))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class StubOpEmitter(val compiler: Compiler, val symbol: CompilableSymbol) : Func

fun exit() {
code.localState.popScope()
code.emit(Opcode.None)
code.emit(Opcode.Return)
}

Expand Down Expand Up @@ -211,6 +212,11 @@ class StubOpEmitter(val compiler: Compiler, val symbol: CompilableSymbol) : Func
}
}

override fun visitReturn(node: Return) {
node.value.visit(this)
code.emit(Opcode.Return)
}

override fun visitSetAssignment(node: SetAssignment) {
val stubVarOrCall = code.localState.resolve(node.symbol)
if (stubVarOrCall.stubVar == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package gay.pizza.pork.evaluator

abstract class BlockFunction {
abstract fun call(): Any
abstract fun call(isFunctionContext: Boolean): Any
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class EvaluationVisitor(root: Scope, val stack: CallStack) : FunctionLevelVisito

scoped(reuseScope, node = node) {
currentScope.define(node.item.symbol.id, item ?: None)
result = blockFunction.call()
result = blockFunction.call(false)
}
} catch (_: BreakMarker) {
break
Expand Down Expand Up @@ -90,7 +90,7 @@ class EvaluationVisitor(root: Scope, val stack: CallStack) : FunctionLevelVisito
if (reuseScope == null) {
reuseScope = currentScope.fork(name = "While")
}
scoped(reuseScope, node = node) { result = blockFunction.call() }
scoped(reuseScope, node = node) { result = blockFunction.call(false) }
} catch (_: BreakMarker) {
break
} catch (_: ContinueMarker) {
Expand Down Expand Up @@ -122,6 +122,8 @@ class EvaluationVisitor(root: Scope, val stack: CallStack) : FunctionLevelVisito
}
}

override fun visitReturn(node: Return): Any = ReturnValue(node.value.visit(this))

private fun unaryNumericOperation(node: PrefixOperation, value: Number) = when (value) {
is Double -> {
unaryNumericOperation(
Expand Down Expand Up @@ -186,10 +188,10 @@ class EvaluationVisitor(root: Scope, val stack: CallStack) : FunctionLevelVisito
val condition = node.condition.visit(this)
return if (condition == true) {
val blockFunction = node.thenBlock.visit(this) as BlockFunction
scoped(node = node) { blockFunction.call() }
scoped(node = node) { blockFunction.call(false) }
} else if (node.elseBlock != null) {
val blockFunction = node.elseBlock!!.visit(this) as BlockFunction
scoped(node = node) { blockFunction.call() }
scoped(node = node) { blockFunction.call(false) }
} else None
}

Expand Down Expand Up @@ -370,10 +372,13 @@ class EvaluationVisitor(root: Scope, val stack: CallStack) : FunctionLevelVisito
override fun visitBlock(node: Block): BlockFunction {
val visitor = this
return object : BlockFunction() {
override fun call(): Any {
override fun call(isFunctionContext: Boolean): Any {
var value: Any? = null
for (expression in node.expressions) {
value = expression.visit(visitor)
if (isFunctionContext && value is ReturnValue) {
return value.value
}
}
return value ?: None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class FunctionContext(val slabContext: SlabContext, val node: FunctionDefinition
stack.push(this)
val blockFunction = visitor.visitBlock(node.block!!)
try {
return blockFunction.call()
return blockFunction.call(true)
} catch (e: PorkError) {
throw e
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package gay.pizza.pork.evaluator

class ReturnValue(val value: Any)
4 changes: 2 additions & 2 deletions examples/fib.pork
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* fibonacci sequence */
func fib(n) {
if n < 2 {
return if n < 2 {
n
} else {
fib(n - 1) + fib(n - 2)
}
}

export func main() {
let result = fib(30)
let result = fib(20)
println(result)
}
4 changes: 2 additions & 2 deletions examples/gameoflife/SDL2.pork
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export func SDL_Quit()
export let SDL_WINDOW_ALLOW_HIGHDPI = 8192

export let SDL_WINDOWPOS_UNDEFINED_MASK = 536805376
export func SDL_WINDOWPOS_UNDEFINED_DISPLAY(x) { SDL_WINDOWPOS_UNDEFINED_MASK | x }
export func SDL_WINDOWPOS_UNDEFINED_DISPLAY(x) { return SDL_WINDOWPOS_UNDEFINED_MASK | x }
export let SDL_WINDOWPOS_UNDEFINED = SDL_WINDOWPOS_UNDEFINED_DISPLAY(0)

export let SDL_WINDOWPOS_CENTERED_MASK = 805240832
export func SDL_WINDOWPOS_CENTERED_DISPLAY(x) { SDL_WINDOWPOS_CENTERED_MASK | x }
export func SDL_WINDOWPOS_CENTERED_DISPLAY(x) { return SDL_WINDOWPOS_CENTERED_MASK | x }
export let SDL_WINDOWPOS_CENTERED = SDL_WINDOWPOS_CENTERED_DISPLAY(0)

export func SDL_CreateWindow(title, x, y, w, h, flags)
Expand Down
8 changes: 4 additions & 4 deletions examples/gameoflife/gameoflife.pork
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ func drawCells(renderer, cells, swap) {

func createCellGrid() {
let numCells = gridWidth * gridHeight
listInitWith(numCells, 0)
return listInitWith(numCells, 0)
}

func getCell(cells, swap, x, y) {
if (x >= 0) and (y >= 0) and (x < gridWidth) and (y < gridHeight) {
let mask = if swap { 2 } else { 1 }
(cells[x + y * gridWidth] & mask) != 0
return (cells[x + y * gridWidth] & mask) != 0
} else {
false
return false
}
}

Expand All @@ -88,7 +88,7 @@ func countNeighbours(cells, swap, x, y) {
if getCell(cells, swap, x - 1, y + 1) { count++ }
if getCell(cells, swap, x - 1, y) { count++ }
if getCell(cells, swap, x - 1, y - 1) { count++ }
count
return count
}

func gameOfLife(cells, swap) {
Expand Down
1 change: 0 additions & 1 deletion examples/numbers.pork
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export func main() {
let pi = 3.141592653589793
println(pi)

}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class ExternalSymbolUsageAnalyzer : FunctionLevelVisitor<Unit>() {
node.visitChildren(this)
}

override fun visitReturn(node: Return) {
node.visitChildren(this)
}

override fun visitSetAssignment(node: SetAssignment) {
node.visitChildren(this)
}
Expand Down
5 changes: 5 additions & 0 deletions parser/src/main/kotlin/gay/pizza/pork/parser/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
TokenType.Break -> parseBreak()
TokenType.Continue -> parseContinue()
TokenType.None -> parseNoneLiteral()
TokenType.Return -> parseReturn()

else -> {
throw ParseError(
Expand Down Expand Up @@ -332,6 +333,10 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
PrefixOperation(ParserHelpers.convertPrefixOperator(it), parseExpression())
}

override fun parseReturn(): Return = expect(NodeType.Return, TokenType.Return) {
Return(parseExpression())
}

override fun parseSetAssignment(): SetAssignment = produce(NodeType.SetAssignment) {
val symbol = parseSymbol()
expect(TokenType.Equals)
Expand Down
5 changes: 5 additions & 0 deletions parser/src/main/kotlin/gay/pizza/pork/parser/Printer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
visit(node.expression)
}

override fun visitReturn(node: Return) {
append("return ")
visit(node.value)
}

override fun visitSuffixOperation(node: SuffixOperation) {
visit(node.reference)
append(node.op.token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ enum class TokenType(vararg val properties: TokenTypeProperty) {
In(ManyChars("in"), KeywordFamily),
Continue(ManyChars("continue"), KeywordFamily),
Break(ManyChars("break"), KeywordFamily),
Return(ManyChars("return"), KeywordFamily),
Import(AnyOf("import", "impork", "porkload"), KeywordFamily),
Export(ManyChars("export"), KeywordFamily),
Func(ManyChars("func"), KeywordFamily),
Expand Down
2 changes: 1 addition & 1 deletion tool/src/main/kotlin/gay/pizza/pork/tool/CompileCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CompileCommand : CliktCommand(help = "Compile Pork to Bytecode", name = "c
if (annotations.isNotEmpty()) {
annotation = " ; ${annotations.joinToString(", ") { it.text}}"
}
println(" ${symbol.offset + index.toUInt()} ${op.code.name} ${op.args.joinToString(" ")}${annotation}")
println(" ${symbol.offset + index.toUInt()} ${op}${annotation}")
}
}
val vm = VirtualMachine(compiledWorld)
Expand Down
2 changes: 2 additions & 0 deletions vm/src/main/kotlin/gay/pizza/pork/vm/VirtualMachine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class VirtualMachine(world: CompiledWorld) : ExecutionContext {
TrueOpHandler,
FalseOpHandler,

NoneOpHandler,

ListMakeOpHandler,
ListSizeOpHandler,

Expand Down
12 changes: 12 additions & 0 deletions vm/src/main/kotlin/gay/pizza/pork/vm/ops/NoneOpHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package gay.pizza.pork.vm.ops

import gay.pizza.pork.bytecode.Op
import gay.pizza.pork.bytecode.Opcode
import gay.pizza.pork.vm.InternalMachine
import gay.pizza.pork.vm.OpHandler

object NoneOpHandler : OpHandler(Opcode.None) {
override fun handle(machine: InternalMachine, op: Op) {
machine.push(Unit)
}
}

0 comments on commit 0a2d029

Please sign in to comment.