Skip to content

Instantly share code, notes, and snippets.

@erodozer
Last active January 18, 2020 18:19
Show Gist options
  • Select an option

  • Save erodozer/a9006881538e7ba6679cf2f52d7e29c5 to your computer and use it in GitHub Desktop.

Select an option

Save erodozer/a9006881538e7ba6679cf2f52d7e29c5 to your computer and use it in GitHub Desktop.
3ch Golf Language Specification
3ch Language Specification
***keywords***
always lower case
var - declare variable
val - declare constant
cls - class
slf - self (this)
sup - super
suc - struct
ifc - interface
ast - abstract
ann - annotation
def - property
get - getter
set - setter
scope
pub - public
pro - protected
prv - private
try - try
exc - except
thr - throw(s)
fun - function
ret - return
cse - case
swt - switch
ift - if then
els - else
eif - else-if
ole - break
ctn - continue
whl - while loop
end - end a block
asy - async
awt - await
/// - comment
***built-in data types***
types and classes are conventionally identifiable by uppercasing first character
Any - inferred
Nil - no value (also used as void for return type)
Err - Exception/Error
primitives
Int - integer (32 bit)
Flt - float (32 bit)
Lng - long (64 bit)
Sht - short (16 bit)
Byt - byte (8 bit)
Bit - bit/bool (1 bit)
Chr - character (16 bit)
collections
Str - string
fmt - format
Arr - Array
<type>... - Typed Array
rmv - remove (value)
rmi - remove (index)
pop - pop
pek - peek
psh - push (alias of append)
fst - first
lst - last
clr - clear
ins - insert @ index
fnd - find
iof - indexOf
get - retrieve from index
Map - key, value mapping
put - put
rmi
Set - set
Itr - iterator
fast built-in math structures
Vc2 - Vector2
Vc3 - Vector3
Mx3 - Matrix 3x3
Mx4 - Matrix 4x4
Shp - 2D shapes
Squ - square
Rct - rectangle
Cir - circle
Tri - triangle
Vol - 3D shapes
Cub - cube
Psm - prism (takes shape and length)
Con - cone
Pyr - pyramid
Sph - sphere
common functions
Mth - math
rnd - round
flr - floor
cel - ceiling
abs - absolute value
sin - sin
cos - cos
tan - tan
exp - exponent
log - log
ran - random
rni - random int
rnd - random decimal
Con - console
log - print to console
err - print error to console
operators
like keywords, operators are lowercase
add - add (+)
sub - subtract (-)
mul - multiply (*)
div - divide (/)
mod - modulo (%)
exp - exponent (^ or **)
ass - assign (=)
not - not (!)
t_s - toString
t_i - toInt
t_f - toFloat
t_b - toBit
t_x - toByte
... - variable argument list (used by parameters, same syntax as an array becuase it is an array, must be last parameter)
lsh - left-shift (<<)
rsh - right-shift (>>)
new - declare an instance of a prototype (called as <type>\new)
comparators
grt - greater than (>)
gte - greater than or equal (>=)
lst - less than (<)
lte - less than or equal (>=)
eql - equal
and - and (&&)
ore - or (||)
xor - xor
nor - not-or
oft - of type
Accessing instance vs prototype values
use backslash (\) to access methods on the instance. You can use backslash on the type to access prototype methods
use period (.) to access properties
ex1.
var x Int...
ass x [1, 2, 3]
Arr\yes(x) /// returns true, same as calling Array.isArray
fun stu(v, idx) /// mapping function, takes value and key/index
ret v\t_s
x\map(stu) /// returns ['1', '2', '3'], same as calling Array.prototype.map
ex2.
suc btl
var foo bit
var bar bit
end
var x btl
ass x btl\new
ass btl.foo 0
ass btl.bar 1
Language Syntax Requirements
operations are performed in prefix notation
example.
ass y rni 1 5
ass x xor eql 5 add 3 y grt y 2
con\log(x)
a top-level function of name "run" is the application's entry method for execution, it can take a variable amount of arguments
Language guidelines
indents should be 3 spaces
lines should not be more than 99 chr long
classes start with a capital letter
functions and properties should be 3 character long but it is not required
functions and properties are lowercase
names should not be more than 9 characters long
Language quirks
you can not declare and assign values at the same time
common number types are supported and can be used to automatically determine type, such as
{num}b for binary
{num}x for hex
{num}_ for octal
a 0b will be a bit, but a 10000000b will be a byte, 1000000000000000b will be an int/sht
true and false are not defined by default, use 0b to get a bit value
variable names can not have numerics in them
the source code can be unicode, but the runtime may not support it, as characters are only 16 bit
parameters are non-mutable
built-in operators are designed with a RISC mindset, and thus should not do more than one operation
this is why there's no declare and assign
this is why there's no functions like inc and dec to be equivalent to ++ and --
where possible, operators and language syntax tries to follow the rule of 3s
key exceptions being
- branch statements like if and while loops, because they're the keyword and a single comparator value
- function signatures, as they're keyword, name, params, and return type
/// implementaiton of fizz-buzz in 3ch
/// while it's not required for function names to be only 3 characters long, it is conventional
fun fzz (x Int) Bit
/// fizz is true when x is a multiple of 3
ret eql 0 mod x 3
end
fun bzz (x Int) Bit
/// buzz is true when x is a multiple of 5
ret eql 0 mod x 5
end
fun fbz (x Int) Nil
var o Str
var yes Bit
ass o ""
ass yes 0b
ift fzz(x)
ass o add o "fizz"
ass yes 1b
end
ift bzz(x)
ass o add o "buzz"
ass yes 1b
end
ift not eql yes 1b
ass o add o x\t_s
end
Con\log(o)
end
fun run (i Int...) Nil
var x Int
var n Int
ass n 0
ass x 0
whl lte x i\len
fbz(i\get(n))
ass x add x 1
end
end
fun run(i Any...) Nil
Con\log("hello world")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment