Skip to content

mathiasj33/SimpleInterpreter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SimpleInterpreter

A simple interpreter written in Python inspired by http://www.craftinginterpreters.com/

It can currently interpret a simple procedural language. The language consists of integer, float, boolean and string variables, arithmetic and logical operations as well as string concatenation, assignment, control structures, and functions. A sample program follows:

fun factorial(x) {
    result := 1

    if x < 0 {
        ret -1
    } else {
        while x > 0 {
            result := result * x
            x := x - 1
        }
        ret result
    }
}

print('7! - 2 = ' # factorial(7) - 2)

fun facrec(x) {
    if x = 0 {
        ret 1
    } else {
        ret facrec(x-1) * x
    }
}

print(facrec(7))
first_part := 'Recursive and iterative computes the same is a __'
print(first_part # facrec(4) = factorial(4) # '__ statement.')


fun calcPi(num_iter) {
    pi := 0
    fun aux(nominator, even, iter) {
        if iter = num_iter {
            ret 4*pi
        }
        if even {
            value := -1/nominator
        } else {
            value := 1/nominator
        }
        pi := pi + value
        ret aux(nominator+2, not even, iter+1)
    }
    ret aux(1, false, 1)
}

print('Pi is approximately: ' # calcPi(100))

About

A simple interpreter written in Python inspired by http://www.craftinginterpreters.com/

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages