Skip to content

CoderFaris/gointerpreter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Interpreter

A tree-walking Go Interpreter written in ... Go :)

Features

  • Integer arithmetic
  • Boolean expressions
  • Variable bindings (let)
  • Conditional expressions (if/else)
  • Functions
  • Closures
  • Recursive functions
  • Arrays
  • Hash maps
  • Strings
  • Built-in functions

Architecture

Source code goes through multiple stages:

Raw input -> Lexer -> Tokens -> Parser -> AST -> Evaluator

Usage/Examples

Basic arithmetic

5+3
((5*3) / (4-2)) * 3

Functions

let a = 5
let b = 4
let add = fn(x, y) { x + y };
let result = add(a, b)
result

Recursive Fibonacci

let fibonacci = fn(x) {
    if (x < 2) {
        x
    } else {
        fibonacci(x - 1) + fibonacci(x - 2)
    }
};

fibonacci(10);

Closures

let newAdder = fn(x) {
    fn(y) { x + y };
};

let addTwo = newAdder(2);

addTwo(5);

Running Tests

To run tests, run the following command (after cloning the project)

  go test ./nameofthefolder (e.g. ./ast)

Run Locally

Clone the project

  git clone https://github.com/CoderFaris/gointerpreter.git

Build the project

  go build main.go

Run the project

  go run main.go

Acknowledgements

This project was inspired by and built while studying the book:

Writing an Interpreter in Go by Thorsten Ball.

About

Interpreter in Go with AST and Pratt Parser

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages