Skip to content

Instantly share code, notes, and snippets.

@MatthiasWalk
Last active December 15, 2019 01:41
Show Gist options
  • Save MatthiasWalk/2a04334fe36894f7b4b16e25a2123e25 to your computer and use it in GitHub Desktop.
Save MatthiasWalk/2a04334fe36894f7b4b16e25a2123e25 to your computer and use it in GitHub Desktop.

George - A simple scripting language

Declaring variables

Mutable variable

var a = 10
var b = 10.1
var c = true
var d = "Hello world"
var f = null

Immuntable

const a = 10
const b = 10.1
const c = true
const d = "Hello world"

Arrays

var a = {"Hello", "World", "Foo", "Bar"}
var b = {1, 2, 3, 4}
var c = [10] //Array of size 10
var d = [] //Array of variable length

a[1] //=> "World"
append(a, "Baz") //Fail, because a is of fixed size and if you were to add an element, it would be of size 5
remove(a, 1) //Fail, because a is of fixed size and if you were to remove index 1, it would be of size 3

d = append(d, a[0])
d = remove(d, 0)

Declaring functions

fun fib(n) {
  if(n <= 1) {
    return n
  }
  
  return fib(n - 2) + fib(n - 1)
}

Operators

Equality

var a = "a"
var b = "b"

a == b //true
a === b //true

a != b //false
a !== b //false
var a = 65
var b = "65"

a == b //true
a === b //false

a != b //false
a !== b //true

Boolean Operators

!true //false
true && true //true
true || false //true

Mathematical Operators

42 + 1337 //1379
1337 - 7 //1330
2 * 2 //4
2 / 2 //1
10 % 3 //1

Binary Operators

2 | 1 //3
10 & 2 //8

Preprocessor

#import "path/to/file.g"

IO

Stdout

out(a)
out("Hello")

Stdin

a = in()
var a = in()

Loops

While

while(n < 10) {
  print("Hello" + n)
}

For

var a = {"Hello", "World"}
var b = 1..length(a) //!Not an array!; this is a range
for(var c in b){
  print(a[c])
}

for(_ in 1..10){
  print("Hello world")
}

Special cases

Ranges

var ch = "g"

if(ch in "a".."z"){
  print("Character is lowercase")
}
var ch = "g"

if (ch in "z" .. "a"){
  print("Character is lowercase")
}

Coalesce

var a = null ?? 2

Inline If

var b = if(a ===2){ "Hello" } else { "World" }

Sample Code

var input = in()
var list = []

while(input !== "stop){
  list = append(list, input)
  printList(list)	
  input = in()
}

fun printList(list){
  out("------------------")
  for(var i in 0..length(list) - 1){
    out(list[i])
  }
  out("------------------")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment