Skip to content

Goto is an interpreted programming language written in go.

License

Notifications You must be signed in to change notification settings

pandeykartikey/goto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Goto

Goto is an interpreted programming language written in go.

License: MIT Go Report Card Release

1. Overview

Goto is a dynamically typed programming language written to support all the scripting requirements. It currently supports the following:

  • Data Types: integer, boolean, string
  • Data Structures: list
  • Arithimetic Operations: +, -, *, /, %, **
  • Comparisons: ==, !=, <, <=, >, >=
  • Logical Operators: !, &&, ||
  • If-Else-If Statements
  • For loops
  • Control Flow Statements continue, break, return
  • Multiple Assigments
  • Operator Precedence Parsing
  • Grouped Expressions
  • Functions
  • Scopes
  • Comments
  • Error Handling
  • Built in Functions: append, print, len

2. Table of Content

3. Installation

Source Installation

To install goto, run the following command:

go get -u github.com/pandeykartikey/goto

Binary Releases

Alternatively, you could install a binary-release, from the release page.

4. Usage

To execute a goto-script, pass the name to the interpreter:

$ goto sample.to

Scripts can be made executable by adding a suitable shebang line:

#!/usr/bin/env goto

To drop into goto-repl, type goto. To exit from repl, just type exit or Ctrl + D.

5. Syntax

5.1 Definitions

Variables are defined using the var keyword, with each line ending with ;.

var a = 3;

A variable must be declared before its usage. It is not necessary to provide a value while declaring a variable.

var a;

5.1.1 Multiple Assignments

Goto supports multiple assignments and declarations.

var a,b,c = 1,2,3;

The datatypes of all the variables need not be the same.

a,b = 1,true;

5.1.2 Scoping

Goto supports hiding of global variable in block constructs

var a = 4;
if true { var a = 5; print(a);} # prints 5
print(a); # prints 4

5.2 Arithmetic operations

Goto supports all the basic arithmetic operations along with ** operator for power. (Inspired from Python)

square = b**2;
remainder = b%2;

5.3 Lists

List is a data structure that organizes items by linear sequence. It can hold multiple types.

var a = [1, true, "array"];

5.3.1 Indexing

Lists are 0-indexed. Elements can be accessed using []. Similar indexing exists for strings.

var a = [1, true, "array"];
a[1] # returns true
a[2][3] # returns "a"

5.4 Builtin functions

Goto currently supports 3 built-in functions:

  1. len: Returns the length of string or a list.

    len("goto") # returns 4

  2. append: appends a token at the end of an array

    var a = [1, 2]; append(a, 3) # a becomes [1, 2, 3]

  3. print: prints the content of parameters to STDOUT. It can take multiple arguments.

    print(1, 2, "goto") Output: 1 2 goto

5.5 Functions

Goto defines function using func followed by an identifier and a parameter list.

func identity(x) {
    return x;
}

5.5.1 Local Functions

You can define local functions inside a block statement with limited scope.

func addTwo(x) {
  func addOne(x) { # addOne() is limited to addTwo()'s scope
    return x + 1;
  }
  x = addOne(x);
  return addOne(x);
}

5.6 If-else statements

Goto supports if-else-if statements.

var a,b,c = 1,2,3;
if a > b { 
  c = a + b; 
} else if a < c {
  c = 20; 
} else {
  c = 30; 
}
print(c); # returns 20

5.7 For-loop statements

Goto has supports for-loop statements.

for var i = 0; i < 10; i = i + 1 {
   i = i + 2;
   print(i);
}

All the three initialization, condition, and update are optional in for loop.

for ;; {
   i = i + 2;
   print(i);
}

5.8 Control flow statements

There are three control flow statements in goto:

  1. continue: It skips all the following statements in a for loop and moves on to the next iteration.

  2. break: It is used to break a for loop.

  3. return: It is used to terminate a function. It may also be used to return values from functions.

5.9 Comments

Single Line comments are supported by goto.

# This is a comment

6. Contributing

If you spot anything that seems wrong, please do report an issue.

If you feel something is missing in goto, consider creating an issue or submitting a pull request.

7. Acknowledgments

This programming language takes inspiration from the book Write an Interpreter in Go.

8. License

Goto is licensed under MIT License.

9. Contact

If you have any queries regarding the project or just want to say hello, feel free to drop a mail at pandeykartikey99@gmail.com.