From ddff6cb3652ab0b5325e962117bc80190a59809b Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Tue, 5 Sep 2023 19:37:23 -0700 Subject: [PATCH] language: today, we killed lambdas --- ast/src/main/ast/pork.yml | 7 --- .../main/kotlin/gay/pizza/pork/ast/Lambda.kt | 29 ---------- .../gay/pizza/pork/ast/NodeCoalescer.kt | 3 - .../kotlin/gay/pizza/pork/ast/NodeType.kt | 1 - .../kotlin/gay/pizza/pork/ast/NodeVisitor.kt | 2 - .../pizza/pork/ast/NodeVisitorExtensions.kt | 1 - .../pizza/pork/evaluator/EvaluationVisitor.kt | 18 ------ examples/syntax.pork | 55 ------------------- .../kotlin/gay/pizza/pork/frontend/World.kt | 7 ++- .../kotlin/gay/pizza/pork/parser/Parser.kt | 24 -------- .../kotlin/gay/pizza/pork/parser/Printer.kt | 28 ---------- .../kotlin/gay/pizza/pork/parser/TokenType.kt | 1 - 12 files changed, 5 insertions(+), 171 deletions(-) delete mode 100644 ast/src/main/kotlin/gay/pizza/pork/ast/Lambda.kt delete mode 100644 examples/syntax.pork diff --git a/ast/src/main/ast/pork.yml b/ast/src/main/ast/pork.yml index 35e796c..9f938b6 100644 --- a/ast/src/main/ast/pork.yml +++ b/ast/src/main/ast/pork.yml @@ -115,13 +115,6 @@ types: values: - name: value type: Int - Lambda: - parent: Expression - values: - - name: arguments - type: List - - name: expressions - type: List ListLiteral: parent: Expression values: diff --git a/ast/src/main/kotlin/gay/pizza/pork/ast/Lambda.kt b/ast/src/main/kotlin/gay/pizza/pork/ast/Lambda.kt deleted file mode 100644 index b97037a..0000000 --- a/ast/src/main/kotlin/gay/pizza/pork/ast/Lambda.kt +++ /dev/null @@ -1,29 +0,0 @@ -// GENERATED CODE FROM PORK AST CODEGEN -package gay.pizza.pork.ast - -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable - -@Serializable -@SerialName("lambda") -class Lambda(val arguments: List, val expressions: List) : Expression() { - override val type: NodeType = NodeType.Lambda - - override fun visitChildren(visitor: NodeVisitor): List = - visitor.visitAll(arguments, expressions) - - override fun visit(visitor: NodeVisitor): T = - visitor.visitLambda(this) - - override fun equals(other: Any?): Boolean { - if (other !is Lambda) return false - return other.arguments == arguments && other.expressions == expressions - } - - override fun hashCode(): Int { - var result = arguments.hashCode() - result = 31 * result + expressions.hashCode() - result = 31 * result + type.hashCode() - return result - } -} diff --git a/ast/src/main/kotlin/gay/pizza/pork/ast/NodeCoalescer.kt b/ast/src/main/kotlin/gay/pizza/pork/ast/NodeCoalescer.kt index 657e973..4fdb1b8 100644 --- a/ast/src/main/kotlin/gay/pizza/pork/ast/NodeCoalescer.kt +++ b/ast/src/main/kotlin/gay/pizza/pork/ast/NodeCoalescer.kt @@ -29,9 +29,6 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor { override fun visitIntLiteral(node: IntLiteral): Unit = handle(node) - override fun visitLambda(node: Lambda): Unit = - handle(node) - override fun visitLetAssignment(node: LetAssignment): Unit = handle(node) diff --git a/ast/src/main/kotlin/gay/pizza/pork/ast/NodeType.kt b/ast/src/main/kotlin/gay/pizza/pork/ast/NodeType.kt index e30bfed..43342e7 100644 --- a/ast/src/main/kotlin/gay/pizza/pork/ast/NodeType.kt +++ b/ast/src/main/kotlin/gay/pizza/pork/ast/NodeType.kt @@ -15,7 +15,6 @@ enum class NodeType(val parent: NodeType? = null) { ImportDeclaration(Declaration), InfixOperation(Expression), IntLiteral(Expression), - Lambda(Expression), LetAssignment(Expression), ListLiteral(Expression), Parentheses(Expression), diff --git a/ast/src/main/kotlin/gay/pizza/pork/ast/NodeVisitor.kt b/ast/src/main/kotlin/gay/pizza/pork/ast/NodeVisitor.kt index ca57eb4..24f5b10 100644 --- a/ast/src/main/kotlin/gay/pizza/pork/ast/NodeVisitor.kt +++ b/ast/src/main/kotlin/gay/pizza/pork/ast/NodeVisitor.kt @@ -20,8 +20,6 @@ interface NodeVisitor { fun visitIntLiteral(node: IntLiteral): T - fun visitLambda(node: Lambda): T - fun visitLetAssignment(node: LetAssignment): T fun visitListLiteral(node: ListLiteral): T diff --git a/ast/src/main/kotlin/gay/pizza/pork/ast/NodeVisitorExtensions.kt b/ast/src/main/kotlin/gay/pizza/pork/ast/NodeVisitorExtensions.kt index b57ad24..31146f1 100644 --- a/ast/src/main/kotlin/gay/pizza/pork/ast/NodeVisitorExtensions.kt +++ b/ast/src/main/kotlin/gay/pizza/pork/ast/NodeVisitorExtensions.kt @@ -14,7 +14,6 @@ fun NodeVisitor.visit(node: Node): T = is If -> visitIf(node) is ImportDeclaration -> visitImportDeclaration(node) is IntLiteral -> visitIntLiteral(node) - is Lambda -> visitLambda(node) is ListLiteral -> visitListLiteral(node) is Parentheses -> visitParentheses(node) is PrefixOperation -> visitPrefixOperation(node) diff --git a/evaluator/src/main/kotlin/gay/pizza/pork/evaluator/EvaluationVisitor.kt b/evaluator/src/main/kotlin/gay/pizza/pork/evaluator/EvaluationVisitor.kt index 16840fa..0a06df7 100644 --- a/evaluator/src/main/kotlin/gay/pizza/pork/evaluator/EvaluationVisitor.kt +++ b/evaluator/src/main/kotlin/gay/pizza/pork/evaluator/EvaluationVisitor.kt @@ -27,24 +27,6 @@ class EvaluationVisitor(root: Scope) : NodeVisitor { override fun visitSymbolReference(node: SymbolReference): Any = currentScope.value(node.symbol.id) - override fun visitLambda(node: Lambda): CallableFunction { - return CallableFunction { arguments -> - currentScope = currentScope.fork() - for ((index, argumentSymbol) in node.arguments.withIndex()) { - currentScope.define(argumentSymbol.id, arguments.values[index]) - } - try { - var value: Any? = null - for (expression in node.expressions) { - value = expression.visit(this) - } - value ?: None - } finally { - currentScope = currentScope.leave() - } - } - } - override fun visitParentheses(node: Parentheses): Any = node.expression.visit(this) diff --git a/examples/syntax.pork b/examples/syntax.pork deleted file mode 100644 index 68ac248..0000000 --- a/examples/syntax.pork +++ /dev/null @@ -1,55 +0,0 @@ -export func main() { - let three = 3 - let two = 2 - - let calculateSimple = { in - (50 + three) * two - } - - let calculateComplex = { in - three + two + 50 - } - - let multiply = { a, b in - a * b - } - - // calculates the result - let calculateSimpleResult = calculateSimple() - let calculateComplexResult = calculateComplex() - let multiplyResult = multiply(50, 50) - - let list = [10, 20, 30] - let trueValue = true - let falseValue = false - - let invert = { value in - !value - } - - let notEqual = { a, b in - a != b - } - - let equal = { a, b in - a == b - } - - let results = [ - calculateSimpleResult, - calculateComplexResult, - multiplyResult, - list, - trueValue, - falseValue, - invert(true), - invert(false), - equal(5, 5), - equal(5, 6), - notEqual(5, 5), - notEqual(5, 6) - ] - - println("results:") - println(results) -} diff --git a/frontend/src/main/kotlin/gay/pizza/pork/frontend/World.kt b/frontend/src/main/kotlin/gay/pizza/pork/frontend/World.kt index 20785e7..b44bbb6 100644 --- a/frontend/src/main/kotlin/gay/pizza/pork/frontend/World.kt +++ b/frontend/src/main/kotlin/gay/pizza/pork/frontend/World.kt @@ -8,11 +8,14 @@ import gay.pizza.pork.parser.TokenStreamSource import gay.pizza.pork.parser.Tokenizer class World(val contentSource: ContentSource) { - private val units = mutableMapOf() + private val internalUnits = mutableMapOf() + + val units: List + get() = internalUnits.values.toList() private fun loadOneUnit(path: String): CompilationUnit { val stableIdentity = contentSource.stableContentIdentity(path) - val cached = units[stableIdentity] + val cached = internalUnits[stableIdentity] if (cached != null) { return cached } diff --git a/parser/src/main/kotlin/gay/pizza/pork/parser/Parser.kt b/parser/src/main/kotlin/gay/pizza/pork/parser/Parser.kt index 2b86bf3..0238208 100644 --- a/parser/src/main/kotlin/gay/pizza/pork/parser/Parser.kt +++ b/parser/src/main/kotlin/gay/pizza/pork/parser/Parser.kt @@ -56,26 +56,6 @@ class Parser(source: PeekableSource, val attribution: NodeAttribution) { } } - private fun readLambda(): Lambda = within { - expect(TokenType.LeftCurly) - val arguments = mutableListOf() - while (!peek(TokenType.In)) { - val symbol = readSymbolRaw() - arguments.add(symbol) - if (next(TokenType.Comma)) { - continue - } else { - break - } - } - expect(TokenType.In) - val items = collect(TokenType.RightCurly) { - readExpression() - } - expect(TokenType.RightCurly) - Lambda(arguments, items) - } - private fun readParentheses(): Parentheses = within { expect(TokenType.LeftParentheses) val expression = readExpression() @@ -128,10 +108,6 @@ class Parser(source: PeekableSource, val attribution: NodeAttribution) { readSymbolCases() } - TokenType.LeftCurly -> { - readLambda() - } - TokenType.LeftParentheses -> { readParentheses() } diff --git a/parser/src/main/kotlin/gay/pizza/pork/parser/Printer.kt b/parser/src/main/kotlin/gay/pizza/pork/parser/Printer.kt index b6df1c5..b5e679a 100644 --- a/parser/src/main/kotlin/gay/pizza/pork/parser/Printer.kt +++ b/parser/src/main/kotlin/gay/pizza/pork/parser/Printer.kt @@ -82,34 +82,6 @@ class Printer(buffer: StringBuilder) : NodeVisitor { visit(node.symbol) } - override fun visitLambda(node: Lambda) { - append("{") - if (node.arguments.isNotEmpty()) { - append(" ") - for ((index, argument) in node.arguments.withIndex()) { - visit(argument) - if (index + 1 != node.arguments.size) { - append(",") - } - append(" ") - } - } else { - append(" ") - } - append("in") - out.increaseIndent() - for (expression in node.expressions) { - appendLine() - visit(expression) - } - - if (node.expressions.isNotEmpty()) { - appendLine() - } - out.decreaseIndent() - append("}") - } - override fun visitParentheses(node: Parentheses) { append("(") visit(node.expression) diff --git a/parser/src/main/kotlin/gay/pizza/pork/parser/TokenType.kt b/parser/src/main/kotlin/gay/pizza/pork/parser/TokenType.kt index f90c183..242cef6 100644 --- a/parser/src/main/kotlin/gay/pizza/pork/parser/TokenType.kt +++ b/parser/src/main/kotlin/gay/pizza/pork/parser/TokenType.kt @@ -24,7 +24,6 @@ enum class TokenType(vararg properties: TokenTypeProperty) { Comma(SingleChar(',')), False(Keyword("false"), KeywordFamily), True(Keyword("true"), KeywordFamily), - In(Keyword("in"), KeywordFamily), If(Keyword("if"), KeywordFamily), Then(Keyword("then"), KeywordFamily), Else(Keyword("else"), KeywordFamily),