language: introduce the requirement to use return to return a value from a function

This commit is contained in:
Alex Zenla 2023-11-21 04:28:46 -08:00
parent 5540918e7c
commit 0a2d029c5c
Signed by: alex
GPG Key ID: C0780728420EBFE5
27 changed files with 115 additions and 19 deletions

View File

@ -5,7 +5,7 @@ A work-in-progress programming language.
```pork ```pork
/* fibonacci sequence */ /* fibonacci sequence */
func fib(n) { func fib(n) {
if n < 2 { return if n < 2 {
n n
} else { } else {
fib(n - 1) + fib(n - 2) fib(n - 1) + fib(n - 2)

View File

@ -300,6 +300,11 @@ types:
Continue: Continue:
parent: Expression parent: Expression
values: [] values: []
Return:
parent: Expression
values:
- name: value
type: Expression
NoneLiteral: NoneLiteral:
parent: Expression parent: Expression
values: [] values: []

View File

@ -36,6 +36,7 @@ digraph A {
type_ForIn [shape=box,label="ForIn"] type_ForIn [shape=box,label="ForIn"]
type_Break [shape=box,label="Break"] type_Break [shape=box,label="Break"]
type_Continue [shape=box,label="Continue"] type_Continue [shape=box,label="Continue"]
type_Return [shape=box,label="Return"]
type_NoneLiteral [shape=box,label="NoneLiteral"] type_NoneLiteral [shape=box,label="NoneLiteral"]
type_NativeFunctionDescriptor [shape=box,label="NativeFunctionDescriptor"] type_NativeFunctionDescriptor [shape=box,label="NativeFunctionDescriptor"]
type_IndexedBy [shape=box,label="IndexedBy"] type_IndexedBy [shape=box,label="IndexedBy"]
@ -69,6 +70,7 @@ digraph A {
type_Expression -> type_ForIn type_Expression -> type_ForIn
type_Expression -> type_Break type_Expression -> type_Break
type_Expression -> type_Continue type_Expression -> type_Continue
type_Expression -> type_Return
type_Expression -> type_NoneLiteral type_Expression -> type_NoneLiteral
type_Expression -> type_IndexedBy type_Expression -> type_IndexedBy
type_Definition -> type_FunctionDefinition type_Definition -> type_FunctionDefinition
@ -116,6 +118,7 @@ digraph A {
type_ForIn -> type_ForInItem [style=dotted] type_ForIn -> type_ForInItem [style=dotted]
type_ForIn -> type_Expression [style=dotted] type_ForIn -> type_Expression [style=dotted]
type_ForIn -> type_Block [style=dotted] type_ForIn -> type_Block [style=dotted]
type_Return -> type_Expression [style=dotted]
type_NativeFunctionDescriptor -> type_Symbol [style=dotted] type_NativeFunctionDescriptor -> type_Symbol [style=dotted]
type_NativeFunctionDescriptor -> type_StringLiteral [style=dotted] type_NativeFunctionDescriptor -> type_StringLiteral [style=dotted]
type_IndexedBy -> type_Expression [style=dotted] type_IndexedBy -> type_Expression [style=dotted]

View File

@ -77,6 +77,9 @@ class NodeCoalescer(val followChildren: Boolean = true, val handler: (Node) -> U
override fun visitPrefixOperation(node: PrefixOperation): Unit = override fun visitPrefixOperation(node: PrefixOperation): Unit =
handle(node) handle(node)
override fun visitReturn(node: Return): Unit =
handle(node)
override fun visitSetAssignment(node: SetAssignment): Unit = override fun visitSetAssignment(node: SetAssignment): Unit =
handle(node) handle(node)

View File

@ -58,6 +58,8 @@ interface NodeParser {
fun parsePrefixOperation(): PrefixOperation fun parsePrefixOperation(): PrefixOperation
fun parseReturn(): Return
fun parseSetAssignment(): SetAssignment fun parseSetAssignment(): SetAssignment
fun parseStringLiteral(): StringLiteral fun parseStringLiteral(): StringLiteral

View File

@ -35,6 +35,7 @@ fun NodeParser.parse(type: NodeType): Node =
NodeType.ForIn -> parseForIn() NodeType.ForIn -> parseForIn()
NodeType.Break -> parseBreak() NodeType.Break -> parseBreak()
NodeType.Continue -> parseContinue() NodeType.Continue -> parseContinue()
NodeType.Return -> parseReturn()
NodeType.NoneLiteral -> parseNoneLiteral() NodeType.NoneLiteral -> parseNoneLiteral()
NodeType.NativeFunctionDescriptor -> parseNativeFunctionDescriptor() NodeType.NativeFunctionDescriptor -> parseNativeFunctionDescriptor()
NodeType.IndexedBy -> parseIndexedBy() NodeType.IndexedBy -> parseIndexedBy()

View File

@ -31,6 +31,7 @@ enum class NodeType(val parent: NodeType? = null) {
NoneLiteral(Expression), NoneLiteral(Expression),
Parentheses(Expression), Parentheses(Expression),
PrefixOperation(Expression), PrefixOperation(Expression),
Return(Expression),
SetAssignment(Expression), SetAssignment(Expression),
StringLiteral(Expression), StringLiteral(Expression),
SuffixOperation(Expression), SuffixOperation(Expression),

View File

@ -52,6 +52,8 @@ interface NodeVisitor<T> {
fun visitPrefixOperation(node: PrefixOperation): T fun visitPrefixOperation(node: PrefixOperation): T
fun visitReturn(node: Return): T
fun visitSetAssignment(node: SetAssignment): T fun visitSetAssignment(node: SetAssignment): T
fun visitStringLiteral(node: StringLiteral): T fun visitStringLiteral(node: StringLiteral): T

View File

@ -32,6 +32,7 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
is ForIn -> visitForIn(node) is ForIn -> visitForIn(node)
is Break -> visitBreak(node) is Break -> visitBreak(node)
is Continue -> visitContinue(node) is Continue -> visitContinue(node)
is Return -> visitReturn(node)
is NoneLiteral -> visitNoneLiteral(node) is NoneLiteral -> visitNoneLiteral(node)
is NativeFunctionDescriptor -> visitNativeFunctionDescriptor(node) is NativeFunctionDescriptor -> visitNativeFunctionDescriptor(node)
is IndexedBy -> visitIndexedBy(node) is IndexedBy -> visitIndexedBy(node)

View File

@ -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
}
}

View File

@ -3,4 +3,12 @@ package gay.pizza.pork.bytecode
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@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(" "))
}
}
}

View File

@ -30,6 +30,7 @@ class StubOpEmitter(val compiler: Compiler, val symbol: CompilableSymbol) : Func
fun exit() { fun exit() {
code.localState.popScope() code.localState.popScope()
code.emit(Opcode.None)
code.emit(Opcode.Return) code.emit(Opcode.Return)
} }
@ -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) { override fun visitSetAssignment(node: SetAssignment) {
val stubVarOrCall = code.localState.resolve(node.symbol) val stubVarOrCall = code.localState.resolve(node.symbol)
if (stubVarOrCall.stubVar == null) { if (stubVarOrCall.stubVar == null) {

View File

@ -1,5 +1,5 @@
package gay.pizza.pork.evaluator package gay.pizza.pork.evaluator
abstract class BlockFunction { abstract class BlockFunction {
abstract fun call(): Any abstract fun call(isFunctionContext: Boolean): Any
} }

View File

@ -28,7 +28,7 @@ class EvaluationVisitor(root: Scope, val stack: CallStack) : FunctionLevelVisito
scoped(reuseScope, node = node) { scoped(reuseScope, node = node) {
currentScope.define(node.item.symbol.id, item ?: None) currentScope.define(node.item.symbol.id, item ?: None)
result = blockFunction.call() result = blockFunction.call(false)
} }
} catch (_: BreakMarker) { } catch (_: BreakMarker) {
break break
@ -90,7 +90,7 @@ class EvaluationVisitor(root: Scope, val stack: CallStack) : FunctionLevelVisito
if (reuseScope == null) { if (reuseScope == null) {
reuseScope = currentScope.fork(name = "While") reuseScope = currentScope.fork(name = "While")
} }
scoped(reuseScope, node = node) { result = blockFunction.call() } scoped(reuseScope, node = node) { result = blockFunction.call(false) }
} catch (_: BreakMarker) { } catch (_: BreakMarker) {
break break
} catch (_: ContinueMarker) { } catch (_: ContinueMarker) {
@ -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) { private fun unaryNumericOperation(node: PrefixOperation, value: Number) = when (value) {
is Double -> { is Double -> {
unaryNumericOperation( unaryNumericOperation(
@ -186,10 +188,10 @@ class EvaluationVisitor(root: Scope, val stack: CallStack) : FunctionLevelVisito
val condition = node.condition.visit(this) val condition = node.condition.visit(this)
return if (condition == true) { return if (condition == true) {
val blockFunction = node.thenBlock.visit(this) as BlockFunction val blockFunction = node.thenBlock.visit(this) as BlockFunction
scoped(node = node) { blockFunction.call() } scoped(node = node) { blockFunction.call(false) }
} else if (node.elseBlock != null) { } else if (node.elseBlock != null) {
val blockFunction = node.elseBlock!!.visit(this) as BlockFunction val blockFunction = node.elseBlock!!.visit(this) as BlockFunction
scoped(node = node) { blockFunction.call() } scoped(node = node) { blockFunction.call(false) }
} else None } else None
} }
@ -370,10 +372,13 @@ class EvaluationVisitor(root: Scope, val stack: CallStack) : FunctionLevelVisito
override fun visitBlock(node: Block): BlockFunction { override fun visitBlock(node: Block): BlockFunction {
val visitor = this val visitor = this
return object : BlockFunction() { return object : BlockFunction() {
override fun call(): Any { override fun call(isFunctionContext: Boolean): Any {
var value: Any? = null var value: Any? = null
for (expression in node.expressions) { for (expression in node.expressions) {
value = expression.visit(visitor) value = expression.visit(visitor)
if (isFunctionContext && value is ReturnValue) {
return value.value
}
} }
return value ?: None return value ?: None
} }

View File

@ -40,7 +40,7 @@ class FunctionContext(val slabContext: SlabContext, val node: FunctionDefinition
stack.push(this) stack.push(this)
val blockFunction = visitor.visitBlock(node.block!!) val blockFunction = visitor.visitBlock(node.block!!)
try { try {
return blockFunction.call() return blockFunction.call(true)
} catch (e: PorkError) { } catch (e: PorkError) {
throw e throw e
} catch (e: Exception) { } catch (e: Exception) {

View File

@ -0,0 +1,3 @@
package gay.pizza.pork.evaluator
class ReturnValue(val value: Any)

View File

@ -1,6 +1,6 @@
/* fibonacci sequence */ /* fibonacci sequence */
func fib(n) { func fib(n) {
if n < 2 { return if n < 2 {
n n
} else { } else {
fib(n - 1) + fib(n - 2) fib(n - 1) + fib(n - 2)
@ -8,6 +8,6 @@ func fib(n) {
} }
export func main() { export func main() {
let result = fib(30) let result = fib(20)
println(result) println(result)
} }

View File

@ -15,11 +15,11 @@ export func SDL_Quit()
export let SDL_WINDOW_ALLOW_HIGHDPI = 8192 export let SDL_WINDOW_ALLOW_HIGHDPI = 8192
export let SDL_WINDOWPOS_UNDEFINED_MASK = 536805376 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_UNDEFINED = SDL_WINDOWPOS_UNDEFINED_DISPLAY(0)
export let SDL_WINDOWPOS_CENTERED_MASK = 805240832 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 let SDL_WINDOWPOS_CENTERED = SDL_WINDOWPOS_CENTERED_DISPLAY(0)
export func SDL_CreateWindow(title, x, y, w, h, flags) export func SDL_CreateWindow(title, x, y, w, h, flags)

View File

@ -56,15 +56,15 @@ func drawCells(renderer, cells, swap) {
func createCellGrid() { func createCellGrid() {
let numCells = gridWidth * gridHeight let numCells = gridWidth * gridHeight
listInitWith(numCells, 0) return listInitWith(numCells, 0)
} }
func getCell(cells, swap, x, y) { func getCell(cells, swap, x, y) {
if (x >= 0) and (y >= 0) and (x < gridWidth) and (y < gridHeight) { if (x >= 0) and (y >= 0) and (x < gridWidth) and (y < gridHeight) {
let mask = if swap { 2 } else { 1 } let mask = if swap { 2 } else { 1 }
(cells[x + y * gridWidth] & mask) != 0 return (cells[x + y * gridWidth] & mask) != 0
} else { } else {
false return false
} }
} }
@ -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 + 1) { count++ }
if getCell(cells, swap, x - 1, y) { count++ } if getCell(cells, swap, x - 1, y) { count++ }
if getCell(cells, swap, x - 1, y - 1) { count++ } if getCell(cells, swap, x - 1, y - 1) { count++ }
count return count
} }
func gameOfLife(cells, swap) { func gameOfLife(cells, swap) {

View File

@ -1,5 +1,4 @@
export func main() { export func main() {
let pi = 3.141592653589793 let pi = 3.141592653589793
println(pi) println(pi)
} }

View File

@ -99,6 +99,10 @@ class ExternalSymbolUsageAnalyzer : FunctionLevelVisitor<Unit>() {
node.visitChildren(this) node.visitChildren(this)
} }
override fun visitReturn(node: Return) {
node.visitChildren(this)
}
override fun visitSetAssignment(node: SetAssignment) { override fun visitSetAssignment(node: SetAssignment) {
node.visitChildren(this) node.visitChildren(this)
} }

View File

@ -38,6 +38,7 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
TokenType.Break -> parseBreak() TokenType.Break -> parseBreak()
TokenType.Continue -> parseContinue() TokenType.Continue -> parseContinue()
TokenType.None -> parseNoneLiteral() TokenType.None -> parseNoneLiteral()
TokenType.Return -> parseReturn()
else -> { else -> {
throw ParseError( throw ParseError(
@ -332,6 +333,10 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
PrefixOperation(ParserHelpers.convertPrefixOperator(it), parseExpression()) PrefixOperation(ParserHelpers.convertPrefixOperator(it), parseExpression())
} }
override fun parseReturn(): Return = expect(NodeType.Return, TokenType.Return) {
Return(parseExpression())
}
override fun parseSetAssignment(): SetAssignment = produce(NodeType.SetAssignment) { override fun parseSetAssignment(): SetAssignment = produce(NodeType.SetAssignment) {
val symbol = parseSymbol() val symbol = parseSymbol()
expect(TokenType.Equals) expect(TokenType.Equals)

View File

@ -154,6 +154,11 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
visit(node.expression) visit(node.expression)
} }
override fun visitReturn(node: Return) {
append("return ")
visit(node.value)
}
override fun visitSuffixOperation(node: SuffixOperation) { override fun visitSuffixOperation(node: SuffixOperation) {
visit(node.reference) visit(node.reference)
append(node.op.token) append(node.op.token)

View File

@ -62,6 +62,7 @@ enum class TokenType(vararg val properties: TokenTypeProperty) {
In(ManyChars("in"), KeywordFamily), In(ManyChars("in"), KeywordFamily),
Continue(ManyChars("continue"), KeywordFamily), Continue(ManyChars("continue"), KeywordFamily),
Break(ManyChars("break"), KeywordFamily), Break(ManyChars("break"), KeywordFamily),
Return(ManyChars("return"), KeywordFamily),
Import(AnyOf("import", "impork", "porkload"), KeywordFamily), Import(AnyOf("import", "impork", "porkload"), KeywordFamily),
Export(ManyChars("export"), KeywordFamily), Export(ManyChars("export"), KeywordFamily),
Func(ManyChars("func"), KeywordFamily), Func(ManyChars("func"), KeywordFamily),

View File

@ -29,7 +29,7 @@ class CompileCommand : CliktCommand(help = "Compile Pork to Bytecode", name = "c
if (annotations.isNotEmpty()) { if (annotations.isNotEmpty()) {
annotation = " ; ${annotations.joinToString(", ") { it.text}}" 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) val vm = VirtualMachine(compiledWorld)

View File

@ -12,6 +12,8 @@ class VirtualMachine(world: CompiledWorld) : ExecutionContext {
TrueOpHandler, TrueOpHandler,
FalseOpHandler, FalseOpHandler,
NoneOpHandler,
ListMakeOpHandler, ListMakeOpHandler,
ListSizeOpHandler, ListSizeOpHandler,

View File

@ -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)
}
}