Goto is an interpreted programming language written in go.
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
- 1. Overview
- 2. Table of Content
- 3. Installation
- 4. Usage
- 5. Syntax
- 6. Contributing
- 7. Acknowledgments
- 8. License
- 9. Contact
To install goto
, run the following command:
go get -u github.com/pandeykartikey/goto
Alternatively, you could install a binary-release, from the release page.
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
.
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;
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;
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
Goto supports all the basic arithmetic operations along with **
operator for power. (Inspired from Python)
square = b**2;
remainder = b%2;
List is a data structure that organizes items by linear sequence. It can hold multiple types.
var a = [1, true, "array"];
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"
Goto currently supports 3 built-in functions:
-
len
: Returns the length of string or a list.len("goto") # returns 4
-
append
: appends a token at the end of an arrayvar a = [1, 2]; append(a, 3) # a becomes [1, 2, 3]
-
print
: prints the content of parameters to STDOUT. It can take multiple arguments.print(1, 2, "goto") Output: 1 2 goto
Goto defines function using func
followed by an identifier and a parameter list.
func identity(x) {
return x;
}
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);
}
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
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);
}
There are three control flow statements in goto:
-
continue
: It skips all the following statements in a for loop and moves on to the next iteration. -
break
: It is used to break a for loop. -
return
: It is used to terminate a function. It may also be used to return values from functions.
Single Line comments are supported by goto.
# This is a comment
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.
This programming language takes inspiration from the book Write an Interpreter in Go.
Goto is licensed under MIT License.
If you have any queries regarding the project or just want to say hello, feel free to drop a mail at [email protected].