Giter Site home page Giter Site logo

immanuelhume / gmm Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 1.11 MB

Bytecode interpreter for a subset of Go (for CS4215 at NUS)

Home Page: https://immanuelhume.github.io/gmm/

ANTLR 1.10% Makefile 0.02% Go 2.54% TypeScript 95.70% Shell 0.01% Nix 0.08% HTML 0.34% JavaScript 0.20%

gmm's Introduction

  __ _ _ __ ___  _ __ ___  
 / _` | '_ ` _ \| '_ ` _ \ 
| (_| | | | | | | | | | | |
 \__, |_| |_| |_|_| |_| |_|
 |___/                     

Go minus minus. A stack-based virtual machine for a small subset of Go.

+------------------+
|BUILDING THIS REPO|
+------------------+

This project was built with NodeJS 21.7 and Antlr 4.12. To run locally,

    git clone [email protected]:immanuelhume/gmm
    cd gmm
    npm i          # install dependencies
    npm run runall # run all test cases

Antlr4 binaries are not required unless you modify the parser or lexer. Its
output is already checked into this repository at antlr/.

To run individual files, run this from the project root:

    npm run run path/to/file.go

A live editor is available at https://immanuelhume.github.io/gmm. It is able to
load the examples in the examples/ folder. To build the editor locally:

    npx vite build

The static files will be placed in ./dist, and can be served up with your
choice of server, e.g.

    python -m http.server 5183 --bind 127.0.0.1 --directory ./dist

And it will be available at http://localhost:5183.

+------------+
|INTRODUCTION|
+------------+

To do anything meaningful we'll have to print things to the console. The hello
world example looks like this:

    func main() {
        dbg("Hello world")
    }

We have a dbg builtin instead of Go's fmt library; dbg behaves exactly like
JavaScript's console.log, but also prints the line number where it was called.
This was mainly a development aid.

The panic function helps us check if things are running properly. Like dbg, it
prints a message but then terminates the program.

    func swap() {
        x, y := 1, 2
        y, x := 2, 1
        if x != 2 || y != 1 {
            panic("expected x, y to be 2, 1 but got", x, y)
        }
    }

The go/pass/ folder contains test cases which are meant to pass. The go/fail
folder holds the opposite. Check them to see the full set of features.

Gmm has three primitive data types: int, float, bool, and string. Ints and
floats are 64 bit. These four types (along with pointers) are the only
primitive types in gmm. Other "primitives" like channels and mutexes are data
structures implemented on top of these types.

+-------+
|GOTCHAS|
+-------+

Integer and float literals are strict. For instance, the literal 42 will always
be interpreted as an int, and 42.0 is always a float. These will lead to
compile errors:

    var x float = 42
    var y int   = 42.0

The parser is not very sophisticated, and certain one-liners are invalid. This
is a purely cosmetic issue and does not affect runtime. These, although valid
in actual Go, will not parse here:

    func main() { dbg("hello world") }

    type node struct { val int; left *node; right *node }

Instead, please place things things between braces in their own lines, like so:

    func main() {
        dbg("hello world")
    }

    type node struct {
        val   int
        left  *node
        right *node
    }

Struct literals, when used, must specify all fields.

    u := node{val: 1}                        // compile error!
    v := node{val: 1, left: nil, right: nil} // all good

Error messages for type errors are quite bad for now.

gmm's People

Contributors

immanuelhume avatar

Watchers

 avatar

gmm's Issues

Stuff to do

Notes

Go files for testing are in ./go. The files in ./go/pass are meant to pass (i.e. finish w/o errors). Use npm run runall to test all of them.

Wrapping up

  • string concat with +
    • add a new binary builtin for BinaryOp.Add for DataType.String
    • string creation handled by strPool in the state
  • null check when deref
    • in eval.ts when handling Opcode.Deref
    • it's nil if the address of the pointer matches that of the global nil
  • boolean negation
  • higher order functions
  • parsing comments
  • a simple web frontend

Todos

  • Remaining stuff to compile @immanuelhume
  • Remaining microcodes
  • Multiple assignments, e.g. a, b := 1, 2
  • Multiple return values
  • debugging builtins (debug, assert, etc.)
  • Remaining binary and unary operations, on various data types
    • Float64, Int64
    • String concat with +
  • string pool
  • comments (parser)
  • frontend integration
    • doesn't have to be sourceacademy, we can just pick whatever is easier
  • dot access and ability to create structs (we need this before we can create mutexes!)
  • make keyword
  • goroutines
  • sleep
  • channels (sending, receiving, making)
    • range, close ?
  • Mutex - Lock, and Unlock
  • defer statement?
  • fix the clone function - should it be recursive? @immanuelhume
  • pointers and the new keyword
  • slices?
  • higher order functions
  • boolean negation
  • variable arguments?
  • brackets parsing @immanuelhume
  • ILoadC should also load ints
  • func literals
  • pointer deref
  • pointer eq/neq (and working with nil)

Resources

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.