Interp - an Interpreted Programming Language Pt.2 - Syntax
Hello! This is Pt. 2 of the Interp series. In this post, let us discuss syntax.
TL:DR It’s very standard and you should be able to continue without reading too much into this post
Numbers
Floats or ints.
1.234 # Float
1234 # Int
Strings
Strings of text.
"my string"
Escape
Strings utilize the standard escape sequences such as ‘\n’, ‘\t’, or ‘\\’.
Variables
Variables are declared with the ‘var’ keyword. variable names start with lower case. They can be strings, numbers, other variables, function calls, booleans, and more.
var my_var = "my string"
var my_other_var = my_var
my_var = 10
Comments
Comments are single line and start with ‘#’.
Print("Hello") # My comment
Math
Mathematics, you know the drill. Neg, Add, Sub, Mul, Div, Mod, Pow and Concat, and Combine are supported.
-x
x + y // Addition
x - y // Subtraction
x * y // Multiplication
x / y // Divsion
x % y // Modulus
x ^ y // Exponentation
x <> y // String concatenation
x & y // List or Dict combination
Assign
You can assign and perform math on the same line.
# These are equal:
x = x + 1
x += 1
Parentheses
Parentheses can change the calling order.
(1 + 2) * 3
Dictionaries
Dictionaries act as objects.
var my_dict = {}
my_dict.x = 1 # String key
my_dict["string key"] = 2 # Also string key
my_dict[1] = 3 # Number key
Lists
Lists are what you would expect.
var my_list = [1, 2, 3]
Print(my_list[2])
Booleans
Booleans are true or false.
true # Literal
x == y # Equate
x != y # Not Equal
x < y # Less Than
x > y # More Than
x <= y # Less Than rr Equal
x >= y # More Than or Equal
not bool_x # Not
bool_x and bool_y # And
bool_x or bool_y # Or
If Else
Conditional, the code executes if the condition is true.
if x == y {
Print("x == y")
} else if y == z {
Print("y == z")
} else {
print("no match")
}
While
Loop while condition is true.
var i = 0
while i < 10 {
Print("I'm inna loop")
i += 1
}
Break and continue supported.
brk # Break
cnt # Continue
Named Loop
You can name loops.
my_loop: while true {
my_other_loop: while true {
brk my_loop
}
}
Functions
Functions first letter is a capital. ‘ret’ returns.
fun Function() {
Print(OtherFunction())
}
fun OtherFunction() {
ret "Hello!"
}
Functions passed into functions
Functions can be passed into other functions.
Function(OtherFunction)
Nested functions
Functions can be nested.
fun fn1() {
var x = 1
fun fn2() {
x = 2
}
fn2()
Print(x) # Prints 2
}
Ref parameters
You can pass ‘ref’ arguments which are references, rather than values.
FunctionThatChangesX(ref x)
void and null
Null means nothing while void is undefined.
void
null
Import
Finally, this is how we import code.
imp "otherfile.it"
You can import from a standard location.
imp "@lib.it"
Execute the above line to see the path.
Multilines
You can add the ‘' to escape a newline.
var long_str = "\
Hello! \n\
World! \n\
"
Done!
This is our core syntax! That’s all for today. :)