pandeykartikey / Goto
Programming Languages
Labels
Projects that are alternatives of or similar to Goto
Goto
Goto is an interpreted programming language written in go.
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
- 1. Overview
- 2. Table of Content
- 3. Installation
- 4. Usage
- 5. Syntax
- 6. Contributing
- 7. Acknowledgments
- 8. License
- 9. Contact
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:
-
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
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:
-
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.
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 [email protected].