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 518ef6b commit 320b31c
Show file tree
Hide file tree
Showing 17 changed files with 358 additions and 181 deletions.
14 changes: 11 additions & 3 deletions main.uzlang
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
ozgaruvchi x -> 5;
ozgaruvchi y -> 10;
chiqar x + y;
ozgaruvchi x -> 10;
ozgaruvchi y -> 20;

chiqar "Hello, UzLang!";

f qoshish(a | b) {
qaytar a + b;
}

ozgaruvchi son -> qoshish(x | y);
chiqar son;
6 changes: 5 additions & 1 deletion src/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ fun main() {
token = lexer.getNextToken()
}

println("Tokens: $tokens") // Debug print

val parser = Parser(tokens)
val program = parser.parse()

println("Parsed program: $program") // Debug print

val interpreter = Interpreter(program)
interpreter.interpret()
}
}
3 changes: 0 additions & 3 deletions src/ast/ASTNode.kt

This file was deleted.

3 changes: 0 additions & 3 deletions src/ast/BinaryOperation.kt

This file was deleted.

3 changes: 0 additions & 3 deletions src/ast/Expression.kt

This file was deleted.

3 changes: 0 additions & 3 deletions src/ast/IfStatement.kt

This file was deleted.

3 changes: 0 additions & 3 deletions src/ast/Number.kt

This file was deleted.

3 changes: 0 additions & 3 deletions src/ast/PrintStatement.kt

This file was deleted.

3 changes: 0 additions & 3 deletions src/ast/Program.kt

This file was deleted.

3 changes: 0 additions & 3 deletions src/ast/Statement.kt

This file was deleted.

3 changes: 0 additions & 3 deletions src/ast/Variable.kt

This file was deleted.

3 changes: 0 additions & 3 deletions src/ast/VariableDeclaration.kt

This file was deleted.

81 changes: 67 additions & 14 deletions src/interpreter/Interpreter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,77 @@ import ast.Number

class Interpreter(private val program: Program) {
private val variables = mutableMapOf<String, Int>()
private val functions = mutableMapOf<String, (List<Int>) -> Int>()
private var returnValue: Int? = null

init {
// Define built-in functions here if necessary
functions["qoshish"] = { args -> args[0] + args[1] }
}

fun interpret() {
for (statement in program.statements) {
execute(statement)
}
program.statements.forEach { executeStatement(it) }
}

private fun execute(statement: Statement) {
private fun executeStatement(statement: Statement) {
when (statement) {
is VariableDeclaration -> {
val value = evaluate(statement.value)
val value = evaluateExpression(statement.value)
variables[statement.name] = value
}
is PrintStatement -> {
val value = evaluate(statement.expression)
val value = evaluateExpression(statement.expression)
println(value)
}
is IfStatement -> {
val condition = evaluateExpression(statement.condition)
if (condition != 0) {
executeBlock(statement.ifBranch)
} else if (statement.elseBranch != null) {
executeBlock(statement.elseBranch)
}
}
is FunctionDefinition -> {
functions[statement.name] = { args ->
// Create a new scope for function execution
val previousVariables = variables.toMap()
statement.parameters.forEachIndexed { index, param ->
variables[param] = args[index]
}
executeBlock(statement.body)
// Capture the return value
val result = returnValue ?: 0
returnValue = null
// Restore previous variable scope
variables.clear()
variables.putAll(previousVariables)
// Return value (if any)
result
}
}
is ReturnStatement -> {
returnValue = evaluateExpression(statement.expression)
}
else -> throw IllegalArgumentException("Unknown statement type: $statement")
}
}

is IfStatement -> TODO()
private fun executeBlock(block: Block) {
block.statements.forEach {
executeStatement(it)
if (returnValue != null) {
return
}
}
}

private fun evaluate(expression: Expression): Int {
private fun evaluateExpression(expression: Expression): Int {
return when (expression) {
is Number -> expression.value
is Variable -> variables[expression.name] ?: throw IllegalArgumentException("Undefined variable: ${expression.name}")
is Variable -> variables[expression.name] ?: throw IllegalArgumentException("Unknown variable: ${expression.name}")
is BinaryOperation -> {
val left = evaluate(expression.left)
val right = evaluate(expression.right)
val left = evaluateExpression(expression.left)
val right = evaluateExpression(expression.right)
when (expression.operator) {
"+" -> left + right
"-" -> left - right
Expand All @@ -42,8 +84,19 @@ class Interpreter(private val program: Program) {
else -> throw IllegalArgumentException("Unknown operator: ${expression.operator}")
}
}

is Number -> TODO()
is FunctionCall -> {
val function = functions[expression.functionName] ?: throw IllegalArgumentException("Unknown function: ${expression.functionName}")
val arguments = expression.arguments.map { evaluateExpression(it) }
function(arguments)
}
is ListExpression -> {
expression.elements.size
}
is StringLiteral -> {
println(expression.value)
0
}
else -> throw IllegalArgumentException("Unknown expression type: $expression")
}
}
}
}
Loading

0 comments on commit 320b31c

Please sign in to comment.