Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
umaarov committed May 27, 2024
1 parent 320b31c commit a6c1d51
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
25 changes: 25 additions & 0 deletions src/ast/Ast.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ast

sealed class Statement

data class VariableDeclaration(val name: String, val value: Expression) : Statement()
data class PrintStatement(val expression: Expression) : Statement()
data class IfStatement(val condition: Expression, val ifBranch: Block, val elseBranch: Block?) : Statement()
data class FunctionDefinition(
val name: String,
val parameters: List<String>,
val body: Block,
val returnStatement: ReturnStatement?
) : Statement()
data class Block(val statements: List<Statement>)
data class ReturnStatement(val expression: Expression) : Statement()

sealed class Expression
data class Number(val value: Int) : Expression()
data class Variable(val name: String) : Expression()
data class BinaryOperation(val left: Expression, val operator: String, val right: Expression) : Expression()
data class FunctionCall(val functionName: String, val arguments: List<Expression>) : Expression()
data class ListExpression(val elements: List<Expression>) : Expression()
data class StringLiteral(val value: String) : Expression()

data class Program(val statements: List<Statement>)
45 changes: 32 additions & 13 deletions src/interpreter/Interpreter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ class Interpreter(private val program: Program) {
when (statement) {
is VariableDeclaration -> {
val value = evaluateExpression(statement.value)
variables[statement.name] = value
variables[statement.name] = value as Int
}

is PrintStatement -> {
val value = evaluateExpression(statement.expression)
println(value)
if (statement.expression is StringLiteral) {
// If it's a string literal, print it without appending 0
println(value)
} else {
// For other expressions, print the value
println(value)
}
}

is IfStatement -> {
val condition = evaluateExpression(statement.condition)
if (condition != 0) {
Expand All @@ -35,6 +43,7 @@ class Interpreter(private val program: Program) {
executeBlock(statement.elseBranch)
}
}

is FunctionDefinition -> {
functions[statement.name] = { args ->
// Create a new scope for function execution
Expand All @@ -53,9 +62,11 @@ class Interpreter(private val program: Program) {
result
}
}

is ReturnStatement -> {
returnValue = evaluateExpression(statement.expression)
returnValue = evaluateExpression(statement.expression) as Int
}

else -> throw IllegalArgumentException("Unknown statement type: $statement")
}
}
Expand All @@ -69,33 +80,41 @@ class Interpreter(private val program: Program) {
}
}

private fun evaluateExpression(expression: Expression): Int {
private fun evaluateExpression(expression: Expression): Any {
// println("Evaluating expression: $expression")
return when (expression) {
is Number -> expression.value
is Variable -> variables[expression.name] ?: throw IllegalArgumentException("Unknown variable: ${expression.name}")
is Variable -> variables[expression.name]
?: throw IllegalArgumentException("Unknown variable: ${expression.name}")

is BinaryOperation -> {
val left = evaluateExpression(expression.left)
val right = evaluateExpression(expression.right)
when (expression.operator) {
"+" -> left + right
"-" -> left - right
"*" -> left * right
"/" -> left / right
"+" -> left as Int + right as Int
"-" -> left as Int - right as Int
"*" -> left as Int * right as Int
"/" -> left as Int / right as Int
else -> throw IllegalArgumentException("Unknown operator: ${expression.operator}")
}
}

is FunctionCall -> {
val function = functions[expression.functionName] ?: throw IllegalArgumentException("Unknown function: ${expression.functionName}")
val arguments = expression.arguments.map { evaluateExpression(it) }
val function = functions[expression.functionName]
?: throw IllegalArgumentException("Unknown function: ${expression.functionName}")
val arguments = expression.arguments.map { evaluateExpression(it) as Int }
function(arguments)
}

is ListExpression -> {
expression.elements.size
}

is StringLiteral -> {
println(expression.value)
0
expression.value
}


else -> throw IllegalArgumentException("Unknown expression type: $expression")
}
}
Expand Down

0 comments on commit a6c1d51

Please sign in to comment.