Giter VIP home page Giter VIP logo

gointerpreter's Introduction

TurtlScript

This is an Interpreter written in Golang, mostly following the book "Writing an Interpreter in Go" by Thorsten Ball. However, there are quite a few differences by now. The interpreter is not finished yet!

For example:

  • ** and % operators
  • bitwise operators
  • && and || operators
  • >= and <= operators
  • Float datatype
  • support for both multiline and single line comments (/* */ and //)
  • support "-" in variable names
  • Quit REPL by using ".quit"
  • Using the interpreter to read files with a .turtls extension

Now let's get into the syntax and features of the language:

There are 6 datatypes:

  • Integer (int64 only)
  • Float (float64 only)
  • Boolean
  • String
  • Hash
  • Array

Syntax

TurtlScript is a dynamically typed language featuring a C-like syntax.

Operators

Arithmetic

Addition: x + y
Subtraction: x - y
Multiplication: x * y
Division: x / y
Modulus: x % y
Exponentiation: x ** y

Comparison

Equal to: x == y
Not Equal: x != y
Greater than: x > y
Less than: x < y
Greater than or equal to: x >= y
Less than or equal to: x <= y

Logical

Logical and: x < 5 && x < 10
Logical or: x < 5 || x < 4
Logical not: !(x < 5 && x < 10)

Bitwise

AND: x & y
OR: x | y
XOR: x ^ y
Zero fill left shift: x << y
Signed right shift: x >> y

Assignment

x = 5

Examples

Assignments:

let age = 16;

let err-essage = "You have to be at least 18 years old to enter!";

let success-essage = "You are allowed to enter!";

let allowed = age >= 18;

Here, the messages are both of type String, while age is of type Integer (int64 under the hood). Adding a ".0" at the end of the 16 would result in age being of type Float (float64). As you can see Variables are declared using the let keyword and that every statement has to end with a semicolon.

Conditionals and Function Calls

if allowed {
    print(success-message);
} else {
    print(err-message);
}

This syntax should feel really familiar because it's nearly the same in many languages. If allowed evaluates to true, the statements inside the braces are executed. In this case, the print function is being called, and the success-message is passed in. Else, the statements between the other braces are executed, printing the err-message.

Function Definitions

let add = fn(x, y) {
    return x + y;
}

In TurtlScript, functions are first-class citizens, so they can be bound to let statements like any other expression. The return keyword can also be omitted. Furthermore, TurtlScript supports higher-order functions and closures.

gointerpreter's People

Contributors

maxtheturtle0 avatar

Watchers

 avatar

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.