mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-02 12:50:55 +00:00
language: introduce the requirement to use return to return a value from a function
This commit is contained in:
parent
5540918e7c
commit
0a2d029c5c
@ -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)
|
||||
|
@ -300,6 +300,11 @@ types:
|
||||
Continue:
|
||||
parent: Expression
|
||||
values: []
|
||||
Return:
|
||||
parent: Expression
|
||||
values:
|
||||
- name: value
|
||||
type: Expression
|
||||
NoneLiteral:
|
||||
parent: Expression
|
||||
values: []
|
||||
|
@ -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"]
|
||||
@ -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
|
||||
@ -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]
|
||||
|
@ -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)
|
||||
|
||||
|
@ -58,6 +58,8 @@ interface NodeParser {
|
||||
|
||||
fun parsePrefixOperation(): PrefixOperation
|
||||
|
||||
fun parseReturn(): Return
|
||||
|
||||
fun parseSetAssignment(): SetAssignment
|
||||
|
||||
fun parseStringLiteral(): StringLiteral
|
||||
|
@ -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()
|
||||
|
@ -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),
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
28
ast/src/main/kotlin/gay/pizza/pork/ast/gen/Return.kt
Normal file
28
ast/src/main/kotlin/gay/pizza/pork/ast/gen/Return.kt
Normal 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
|
||||
}
|
||||
}
|
@ -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(" "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
|
@ -1,5 +1,5 @@
|
||||
package gay.pizza.pork.evaluator
|
||||
|
||||
abstract class BlockFunction {
|
||||
abstract fun call(): Any
|
||||
abstract fun call(isFunctionContext: Boolean): Any
|
||||
}
|
||||
|
@ -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
|
||||
@ -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) {
|
||||
@ -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(
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -0,0 +1,3 @@
|
||||
package gay.pizza.pork.evaluator
|
||||
|
||||
class ReturnValue(val value: Any)
|
@ -1,6 +1,6 @@
|
||||
/* fibonacci sequence */
|
||||
func fib(n) {
|
||||
if n < 2 {
|
||||
return if n < 2 {
|
||||
n
|
||||
} else {
|
||||
fib(n - 1) + fib(n - 2)
|
||||
@ -8,6 +8,6 @@ func fib(n) {
|
||||
}
|
||||
|
||||
export func main() {
|
||||
let result = fib(30)
|
||||
let result = fib(20)
|
||||
println(result)
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
|
@ -1,5 +1,4 @@
|
||||
export func main() {
|
||||
let pi = 3.141592653589793
|
||||
println(pi)
|
||||
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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(
|
||||
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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),
|
||||
|
@ -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)
|
||||
|
@ -12,6 +12,8 @@ class VirtualMachine(world: CompiledWorld) : ExecutionContext {
|
||||
TrueOpHandler,
|
||||
FalseOpHandler,
|
||||
|
||||
NoneOpHandler,
|
||||
|
||||
ListMakeOpHandler,
|
||||
ListSizeOpHandler,
|
||||
|
||||
|
12
vm/src/main/kotlin/gay/pizza/pork/vm/ops/NoneOpHandler.kt
Normal file
12
vm/src/main/kotlin/gay/pizza/pork/vm/ops/NoneOpHandler.kt
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user