Initial Commit

This commit is contained in:
2023-08-19 15:29:07 -07:00
commit 6494858f02
43 changed files with 1240 additions and 0 deletions

View File

@ -0,0 +1,3 @@
package gay.pizza.pork
object PorkLanguage

View File

@ -0,0 +1,5 @@
package gay.pizza.pork.ast
class BooleanLiteral(val value: Boolean) : Expression {
override val type: NodeType = NodeType.BooleanLiteral
}

View File

@ -0,0 +1,8 @@
package gay.pizza.pork.ast
class Define(val symbol: Symbol, val value: Expression) : Expression {
override val type: NodeType = NodeType.Define
override fun <T> visitChildren(visitor: Visitor<T>): List<T> =
listOf(visitor.visit(symbol), visitor.visit(value))
}

View File

@ -0,0 +1,3 @@
package gay.pizza.pork.ast
interface Expression : Node

View File

@ -0,0 +1,8 @@
package gay.pizza.pork.ast
class FunctionCall(val symbol: Symbol) : Expression {
override val type: NodeType = NodeType.FunctionCall
override fun <T> visitChildren(visitor: Visitor<T>): List<T> =
listOf(visitor.visit(symbol))
}

View File

@ -0,0 +1,8 @@
package gay.pizza.pork.ast
class InfixOperation(val left: Expression, val op: InfixOperator, val right: Expression) : Expression {
override val type: NodeType = NodeType.InfixOperation
override fun <T> visitChildren(visitor: Visitor<T>): List<T> =
listOf(visitor.visit(left), visitor.visit(right))
}

View File

@ -0,0 +1,8 @@
package gay.pizza.pork.ast
enum class InfixOperator(val token: String) {
Plus("+"),
Minus("-"),
Multiply("*"),
Divide("/")
}

View File

@ -0,0 +1,5 @@
package gay.pizza.pork.ast
class IntLiteral(val value: Int) : Expression {
override val type: NodeType = NodeType.IntLiteral
}

View File

@ -0,0 +1,10 @@
package gay.pizza.pork.ast
class Lambda(val expressions: List<Expression>) : Expression {
constructor(vararg expressions: Expression) : this(listOf(*expressions))
override val type: NodeType = NodeType.Lambda
override fun <T> visitChildren(visitor: Visitor<T>): List<T> =
expressions.map { expression -> visitor.visit(expression) }
}

View File

@ -0,0 +1,10 @@
package gay.pizza.pork.ast
class ListLiteral(val items: List<Expression>) : Expression {
constructor(vararg items: Expression) : this(listOf(*items))
override val type: NodeType = NodeType.ListLiteral
override fun <T> visitChildren(visitor: Visitor<T>): List<T> =
items.map { visitor.visit(it) }
}

View File

@ -0,0 +1,6 @@
package gay.pizza.pork.ast
interface Node {
val type: NodeType
fun <T> visitChildren(visitor: Visitor<T>): List<T> = emptyList()
}

View File

@ -0,0 +1,37 @@
package gay.pizza.pork.ast
import gay.pizza.pork.ast.NodeTypeTrait.*
enum class NodeType(val parent: NodeType? = null, vararg traits: NodeTypeTrait) {
Node,
Symbol(Node),
Expression(Node, Intermediate),
Program(Node),
IntLiteral(Expression, Literal),
BooleanLiteral(Expression, Literal),
ListLiteral(Expression, Literal),
Parentheses(Expression),
Define(Expression),
Lambda(Expression),
InfixOperation(Expression),
SymbolReference(Expression),
FunctionCall(Expression);
val parents: Set<NodeType>
init {
val calculatedParents = mutableListOf<NodeType>()
var self = this
while (true) {
calculatedParents.add(self)
if (self.parent != null) {
self = self.parent!!
} else {
break
}
}
parents = calculatedParents.toSet()
}
fun isa(type: NodeType): Boolean = this == type || parents.contains(type)
}

View File

@ -0,0 +1,6 @@
package gay.pizza.pork.ast
enum class NodeTypeTrait {
Intermediate,
Literal
}

View File

@ -0,0 +1,8 @@
package gay.pizza.pork.ast
class Parentheses(val expression: Expression) : Expression {
override val type: NodeType = NodeType.Parentheses
override fun <T> visitChildren(visitor: Visitor<T>): List<T> =
listOf(visitor.visit(expression))
}

View File

@ -0,0 +1,100 @@
package gay.pizza.pork.ast
class Printer(private val buffer: StringBuilder) : Visitor<Unit> {
private var indent = 0
private fun append(text: String) {
buffer.append(text)
}
private fun appendLine() {
buffer.appendLine()
}
private fun indent() {
repeat(indent) {
append(" ")
}
}
override fun visitDefine(node: Define) {
visit(node.symbol)
append(" = ")
visit(node.value)
}
override fun visitFunctionCall(node: FunctionCall) {
visit(node.symbol)
append("()")
}
override fun visitReference(node: SymbolReference) {
visit(node.symbol)
}
override fun visitSymbol(node: Symbol) {
append(node.id)
}
override fun visitLambda(node: Lambda) {
append("{")
indent++
for (expression in node.expressions) {
appendLine()
indent()
visit(expression)
}
if (node.expressions.isNotEmpty()) {
appendLine()
}
indent--
indent()
append("}")
}
override fun visitIntLiteral(node: IntLiteral) {
append(node.value.toString())
}
override fun visitBooleanLiteral(node: BooleanLiteral) {
if (node.value) {
append("true")
} else {
append("false")
}
}
override fun visitListLiteral(node: ListLiteral) {
append("[")
for ((index, item) in node.items.withIndex()) {
visit(item)
if (index != node.items.size - 1) {
append(", ")
}
}
append("]")
}
override fun visitParentheses(node: Parentheses) {
append("(")
visit(node.expression)
append(")")
}
override fun visitInfixOperation(node: InfixOperation) {
visit(node.left)
append(" ")
append(node.op.token)
append(" ")
visit(node.right)
}
override fun visitProgram(node: Program) {
for (expression in node.expressions) {
indent()
visit(expression)
appendLine()
}
}
}

View File

@ -0,0 +1,10 @@
package gay.pizza.pork.ast
class Program(val expressions: List<Expression>) : Node {
constructor(vararg expressions: Expression) : this(listOf(*expressions))
override val type: NodeType = NodeType.Program
override fun <T> visitChildren(visitor: Visitor<T>): List<T> =
expressions.map { visitor.visit(it) }
}

View File

@ -0,0 +1,5 @@
package gay.pizza.pork.ast
class Symbol(val id: String) : Node {
override val type: NodeType = NodeType.Symbol
}

View File

@ -0,0 +1,8 @@
package gay.pizza.pork.ast
class SymbolReference(val symbol: Symbol) : Expression {
override val type: NodeType = NodeType.SymbolReference
override fun <T> visitChildren(visitor: Visitor<T>): List<T> =
listOf(visitor.visit(symbol))
}

View File

@ -0,0 +1,38 @@
package gay.pizza.pork.ast
interface Visitor<T> {
fun visitDefine(node: Define): T
fun visitFunctionCall(node: FunctionCall): T
fun visitReference(node: SymbolReference): T
fun visitSymbol(node: Symbol): T
fun visitLambda(node: Lambda): T
fun visitIntLiteral(node: IntLiteral): T
fun visitBooleanLiteral(node: BooleanLiteral): T
fun visitListLiteral(node: ListLiteral): T
fun visitParentheses(node: Parentheses): T
fun visitInfixOperation(node: InfixOperation): T
fun visitProgram(node: Program): T
fun visitExpression(node: Expression): T = when (node) {
is IntLiteral -> visitIntLiteral(node)
is BooleanLiteral -> visitBooleanLiteral(node)
is ListLiteral -> visitListLiteral(node)
is Parentheses -> visitParentheses(node)
is InfixOperation -> visitInfixOperation(node)
is Define -> visitDefine(node)
is Lambda -> visitLambda(node)
is FunctionCall -> visitFunctionCall(node)
is SymbolReference -> visitReference(node)
else -> throw RuntimeException("Unknown Expression")
}
fun visit(node: Node): T = when (node) {
is Expression -> visitExpression(node)
is Symbol -> visitSymbol(node)
is Program -> visitProgram(node)
else -> throw RuntimeException("Unknown Node")
}
}

View File

@ -0,0 +1,46 @@
package gay.pizza.pork.eval
import java.util.function.Function
class Context(val parent: Context? = null) {
private val variables = mutableMapOf<String, Any>()
fun define(name: String, value: Any) {
if (variables.containsKey(name)) {
throw RuntimeException("Variable '${name}' is already defined.")
}
variables[name] = value
}
fun value(name: String): Any {
val value = variables[name]
if (value == null) {
if (parent != null) {
return parent.value(name)
}
throw RuntimeException("Variable '${name}' not defined.")
}
return value
}
fun call(name: String, argument: Any = Unit): Any {
val value = value(name)
if (value !is Function<*, *>) {
throw RuntimeException("$value is not callable.")
}
@Suppress("UNCHECKED_CAST")
val casted = value as Function<Any, Any>
return casted.apply(argument)
}
fun fork(): Context {
return Context(this)
}
fun leave(): Context {
if (parent == null) {
throw RuntimeException("Parent context not found.")
}
return parent
}
}

View File

@ -0,0 +1,71 @@
package gay.pizza.pork.eval
import gay.pizza.pork.ast.*
import java.util.function.Function
class Evaluator(root: Context) : Visitor<Any> {
private var currentContext: Context = root
override fun visitDefine(node: Define): Any {
val value = visit(node.value)
currentContext.define(node.symbol.id, value)
return value
}
override fun visitFunctionCall(node: FunctionCall): Any = currentContext.call(node.symbol.id)
override fun visitReference(node: SymbolReference): Any =
currentContext.value(node.symbol.id)
override fun visitSymbol(node: Symbol): Any {
return Unit
}
override fun visitLambda(node: Lambda): Function<Any, Any> {
return Function { _ ->
currentContext = currentContext.fork()
try {
var value: Any? = null
for (expression in node.expressions) {
value = visit(expression)
}
value ?: Unit
} finally {
currentContext = currentContext.leave()
}
}
}
override fun visitIntLiteral(node: IntLiteral): Any = node.value
override fun visitBooleanLiteral(node: BooleanLiteral): Any = node.value
override fun visitListLiteral(node: ListLiteral): Any = node.items.map { visit(it) }
override fun visitParentheses(node: Parentheses): Any = visit(node.expression)
override fun visitInfixOperation(node: InfixOperation): Any {
val left = visit(node.left)
val right = visit(node.right)
if (left !is Number || right !is Number) {
throw RuntimeException("Failed to evaluate infix operation, bad types.")
}
val leftInt = left.toInt()
val rightInt = right.toInt()
return when (node.op) {
InfixOperator.Plus -> leftInt + rightInt
InfixOperator.Minus -> leftInt - rightInt
InfixOperator.Multiply -> leftInt * rightInt
InfixOperator.Divide -> leftInt / rightInt
}
}
override fun visitProgram(node: Program): Any {
var value: Any? = null
for (expression in node.expressions) {
value = visit(expression)
}
return value ?: Unit
}
}

View File

@ -0,0 +1,25 @@
package gay.pizza.pork
import gay.pizza.pork.ast.*
import gay.pizza.pork.eval.Context
import gay.pizza.pork.eval.Evaluator
import gay.pizza.pork.parse.*
import kotlin.io.path.Path
import kotlin.io.path.readText
fun main(args: Array<String>) {
fun eval(ast: Program) {
val context = Context()
val evaluator = Evaluator(context)
evaluator.visit(ast)
println("> ${context.call("main")}")
}
val code = Path(args[0]).readText()
val tokenizer = PorkTokenizer(StringCharSource(code))
val stream = tokenizer.tokenize()
println(stream.tokens.joinToString("\n"))
val parser = PorkParser(TokenStreamSource(stream))
val program = parser.readProgram()
eval(program)
}

View File

@ -0,0 +1,8 @@
package gay.pizza.pork.parse
interface CharSource : PeekableSource<Char> {
companion object {
@Suppress("ConstPropertyName")
const val NullChar = 0.toChar()
}
}

View File

@ -0,0 +1,7 @@
package gay.pizza.pork.parse
interface PeekableSource<T> {
val currentIndex: Int
fun next(): T
fun peek(): T
}

View File

@ -0,0 +1,125 @@
package gay.pizza.pork.parse
import gay.pizza.pork.ast.*
class PorkParser(val source: PeekableSource<Token>) {
private fun readIntLiteral(): IntLiteral {
val token = expect(TokenType.IntLiteral)
return IntLiteral(token.text.toInt())
}
private fun readSymbol(): Symbol {
val token = expect(TokenType.Symbol)
return Symbol(token.text)
}
private fun readSymbolCases(): Expression {
val symbol = readSymbol()
return if (peekType(TokenType.LeftParentheses)) {
expect(TokenType.LeftParentheses)
expect(TokenType.RightParentheses)
FunctionCall(symbol)
} else if (peekType(TokenType.Equals)) {
expect(TokenType.Equals)
Define(symbol, readExpression())
} else {
SymbolReference(symbol)
}
}
fun readLambda(): Lambda {
expect(TokenType.LeftCurly)
val items = collectExpressions(TokenType.RightCurly)
expect(TokenType.RightCurly)
return Lambda(items)
}
fun readExpression(): Expression {
val token = source.peek()
val expression = when (token.type) {
TokenType.IntLiteral -> {
readIntLiteral()
}
TokenType.LeftBracket -> {
readListLiteral()
}
TokenType.Symbol -> {
readSymbolCases()
}
TokenType.LeftCurly -> {
readLambda()
}
TokenType.LeftParentheses -> {
expect(TokenType.LeftParentheses)
val expression = readExpression()
expect(TokenType.RightParentheses)
Parentheses(expression)
}
TokenType.True -> {
expect(TokenType.True)
return BooleanLiteral(true)
}
TokenType.False -> {
expect(TokenType.False)
return BooleanLiteral(false)
}
else -> {
throw RuntimeException("Failed to parse token: ${token.type} '${token.text}' as expression.")
}
}
if (peekType(TokenType.Plus, TokenType.Minus, TokenType.Multiply, TokenType.Divide)) {
val infixToken = source.next()
val infixOperator = convertInfixOperator(infixToken)
return InfixOperation(expression, infixOperator, readExpression())
}
return expression
}
private fun convertInfixOperator(token: Token): InfixOperator =
when (token.type) {
TokenType.Plus -> InfixOperator.Plus
TokenType.Minus -> InfixOperator.Minus
TokenType.Multiply -> InfixOperator.Multiply
TokenType.Divide -> InfixOperator.Divide
else -> throw RuntimeException("Unknown Infix Operator")
}
fun readListLiteral(): ListLiteral {
expect(TokenType.LeftBracket)
val items = collectExpressions(TokenType.RightBracket, TokenType.Comma)
expect(TokenType.RightBracket)
return ListLiteral(items)
}
fun readProgram(): Program {
val items = collectExpressions(TokenType.EndOfFile)
expect(TokenType.EndOfFile)
return Program(items)
}
private fun collectExpressions(peeking: TokenType, consuming: TokenType? = null): List<Expression> {
val items = mutableListOf<Expression>()
while (!peekType(peeking)) {
val expression = readExpression()
if (consuming != null && !peekType(peeking)) {
expect(consuming)
}
items.add(expression)
}
return items
}
private fun peekType(vararg types: TokenType): Boolean {
val token = source.peek()
return types.contains(token.type)
}
private fun expect(type: TokenType): Token {
val token = source.next()
if (token.type != type) {
throw RuntimeException("Expected token type '${type}' but got type ${token.type} '${token.text}'")
}
return token
}
}

View File

@ -0,0 +1,87 @@
package gay.pizza.pork.parse
class PorkTokenizer(val source: CharSource) {
private var tokenStart: Int = 0
private fun isSymbol(c: Char): Boolean =
(c in 'a'..'z') || (c in 'A'..'Z') || c == '_'
private fun isDigit(c: Char): Boolean =
c in '0'..'9'
private fun isWhitespace(c: Char): Boolean =
c == ' ' || c == '\r' || c == '\n' || c == '\t'
private fun readSymbolOrKeyword(firstChar: Char): Token {
val symbol = buildString {
append(firstChar)
while (isSymbol(source.peek())) {
append(source.next())
}
}
var type = TokenType.Symbol
for (keyword in TokenType.Keywords) {
if (symbol == keyword.keyword) {
type = keyword
}
}
return Token(type, symbol)
}
private fun readIntLiteral(firstChar: Char): Token {
val number = buildString {
append(firstChar)
while (isDigit(source.peek())) {
append(source.next())
}
}
return Token(TokenType.IntLiteral, number)
}
private fun skipWhitespace() {
while (isWhitespace(source.peek())) {
source.next()
}
}
fun next(): Token {
while (source.peek() != CharSource.NullChar) {
tokenStart = source.currentIndex
val char = source.next()
for (item in TokenType.SingleChars) {
if (item.singleChar == char) {
return Token(item, char.toString())
}
}
if (isWhitespace(char)) {
skipWhitespace()
continue
}
if (isDigit(char)) {
return readIntLiteral(char)
}
if (isSymbol(char)) {
return readSymbolOrKeyword(char)
}
throw RuntimeException("Failed to parse: (${char}) next ${source.peek()}")
}
return TokenSource.EndOfFile
}
fun tokenize(): TokenStream {
val tokens = mutableListOf<Token>()
while (true) {
val token = next()
tokens.add(token)
if (token.type == TokenType.EndOfFile) {
break
}
}
return TokenStream(tokens)
}
}

View File

@ -0,0 +1,22 @@
package gay.pizza.pork.parse
class StringCharSource(val input: String) : CharSource {
private var index = 0
override val currentIndex: Int = index
override fun next(): Char {
if (index == input.length) {
return CharSource.NullChar
}
val char = input[index]
index++
return char
}
override fun peek(): Char {
if (index == input.length) {
return CharSource.NullChar
}
return input[index]
}
}

View File

@ -0,0 +1,5 @@
package gay.pizza.pork.parse
class Token(val type: TokenType, val text: String) {
override fun toString(): String = "${type.name} $text"
}

View File

@ -0,0 +1,7 @@
package gay.pizza.pork.parse
interface TokenSource : PeekableSource<Token> {
companion object {
val EndOfFile = Token(TokenType.EndOfFile, "")
}
}

View File

@ -0,0 +1,5 @@
package gay.pizza.pork.parse
class TokenStream(val tokens: List<Token>) {
override fun toString(): String = tokens.toString()
}

View File

@ -0,0 +1,22 @@
package gay.pizza.pork.parse
class TokenStreamSource(val stream: TokenStream) : TokenSource {
private var index = 0
override val currentIndex: Int = index
override fun next(): Token {
if (index == stream.tokens.size) {
return TokenSource.EndOfFile
}
val char = stream.tokens[index]
index++
return char
}
override fun peek(): Token {
if (index == stream.tokens.size) {
return TokenSource.EndOfFile
}
return stream.tokens[index]
}
}

View File

@ -0,0 +1,26 @@
package gay.pizza.pork.parse
enum class TokenType(val singleChar: Char? = null, val keyword: String? = null) {
Symbol,
IntLiteral,
Equals(singleChar = '='),
Plus(singleChar = '+'),
Minus(singleChar = '-'),
Multiply(singleChar = '*'),
Divide(singleChar = '/'),
LeftCurly(singleChar = '{'),
RightCurly(singleChar = '}'),
LeftBracket(singleChar = '['),
RightBracket(singleChar = ']'),
LeftParentheses(singleChar = '('),
RightParentheses(singleChar = ')'),
Comma(singleChar = ','),
False(keyword = "false"),
True(keyword = "true"),
EndOfFile;
companion object {
val Keywords = entries.filter { it.keyword != null }
val SingleChars = entries.filter { it.singleChar != null }
}
}