This project is a simple interpreter written in Haskell. It is designed to evaluate expressions and demonstrate the power of functional programming.
The language that is interpreted is called AHA, it's syntax is block based denoted with brackets and is pretty simple.
- Basic arithmetic operations
- Variable assignments
- Simple conditional
- Printing values
- Looping (for loop, do loop, while loop)
- Conditional operators (<,>,>=,<=)
- Lists and basic operations (pop, append, remove and add)
- Comments
- Function definitions
- Basic logical operation
- Basic module handling
- Loop flow control (haltLoop, next, next if, next unless)
- Ranges
- Objects
- String interpolation
Here are some examples of expressions you can evaluate with the interpreter:
1 + 2 * 3 / 2
x = 2
x = 10
y = 20
if x {
result = x + y
} else {
result = 0
}
x = 10
y = "Hello"
print x
print y
z = y + " World"
print z
for (x = 0; x < 10; x = x + 1) {
print x
}
print "Greater than"
print 5 > 1
print 5 > 6
print "Less than"
print 5 < 1
print 5 < 6
print "Equal to"
print 5 == 1
print 5 == 6
print "Not equal to"
print 5 != 1
print 5 != 6
print "Greater than or equal to"
print 5 >= 1
print 5 >= 6
print "Less than or equal to"
print 5 <= 1
print 5 <= 6
x = [1, 2, 3]
y = x[1]
print y
print x
x = x << "hellows world" # appends value to the end of list
print x
x = x >> 0 # Deletes the item at position 0 from the list
z = x =>> 0 # Pops the item at position 0, returning the element
print x
print z
x = x << 1 <<= 5 # Add 5 to the list, placing it a the index 1 position
print x
w = 1..10
print w
w = 1..10 stepping 2
print w
# This is a comment
/*
This is a
multiline
comment :)
*/
def add(x, y) {
return x + y
}
def sayHello(){
print "Hello World"
}
x = 5
y = 10
z = add(x, y)
sayHello()
print z
print true and false
print true and true
print true or true
print false or false
print false or true
print not false
print not true
import ./example/funcs
print "calling a function of the funcs.aha file, inside imports.aha 👀"
print add(1,2)
x = 1
do {
print x
x = x + 1
} loop (x < 0)
y = 1
loop (y < 5) {
print y
y = y + 1
}
loop (true) {
print "once"
haltLoop
}
for (i = 0; i < 5; i = i + 1) {
next if i < 4
print i
}
for (i = 0; i < 5; i = i + 1) {
next unless i < 3
print i
}
for (i = 0; i < 5; i = i + 1) {
next
print i
}
x = {
hello: "world",
name: "this",
"anotherObject": {
test: 2
}
}
print x
print x.hello
print x.anotherObject.test
print {hello: 2}.hello
x.adding = "added"
x.anotherObject.adding = "this also adds"
x.anotherObject.objectAdded = {
"objectAdd": 1
}
print x
x = "interpolation"
print f"string {x}"
print f"1+1={1+1} {1}"