Oak Programming Language Specification
Syntax
Oak, like Ink, has automatic comma insertion at end of lines. This means if a comma can be inserted at the end of a line, it will automatically be inserted.
program := expr*
expr := literal | identifier |
assignment |
propertyAccess |
unaryExpr | binaryExpr |
prefixCall | infixCall |
ifExpr | withExpr |
block
literal := nullLiteral |
numberLiteral | stringLiteral | atomLiteral | boolLiteral |
listLiteral | objectLiteral |
fnLiterael
nullLiteral := '?'
numberLiteral := \d+ | \d* '.' \d+
stringLiteral := // single quoted string with standard escape sequences + \x00 syntax
atomLiteral := ':' + identifier
boolLiteral := 'true' | 'false'
listLiteral := '[' ( expr ',' )* ']' // last comma optional
objectLiteral := '{' ( expr ':' expr ',' )* '}' // last comma optional
fnLiteral := 'fn' '(' ( identifier ',' )* (identifier '...')? ')' expr
identifier := \w_ (\w\d_?!)* | _
assignment := (
identifier [':=' '<-'] expr |
listLiteral [':=' '<-'] expr |
objectLiteral [':=' '<-'] expr
)
propertyAccess := identifier ('.' identifier)+
unaryExpr := ('!' | '-') expr
binaryExpr := expr (+ - * / % ^ & | > < = >= <= <<) binaryExpr
prefixCall := expr '(' (expr ',')* ')'
infixCall := expr '|>' prefixCall
ifExpr := 'if' expr? '{' ifClause* '}'
ifClause := expr '->' expr ','
withExpr := 'with' prefixCall fnLiteral
block := '{' expr+ '}' | '(' expr* ')'AST node types
nullLiteral
stringLiteral
numberLiteral
boolLiteral
atomLiteral
listLiteral
objectLiteral
fnLiteral
identifier
assignment
propertyAccess
unaryExpr
binaryExpr
fnCall
ifExpr
blockLanguage Functions
import(path): Imports a module located at the specifiedpath.string(x): Converts the argumentxto a string.int(x): Converts the argumentxto an integer.float(x): Converts the argumentxto a floating-point number.atom(c): Creates an atom with the specified characterc.codepoint(c): Returns the Unicode code point of the characterc.char(n): Converts the Unicode code pointnto a character.type(x): Returns the type of the argumentx.len(x): Returns the length of the argumentx.keys(x): Returns an array of keys of the argumentx.
OS Functions
args(): Returns command-line arguments as an array of strings.env(): Returns the environment variables as an object.time(): Returns the current time as a float.nanotime(): Returns the current time in nanoseconds as an integer.exit(code): Exits the program with the specified exit code.rand(): Generates a random floating-point number between 0 and 1.srand(length): Seeds the random number generator with the specified length.wait(duration): Pauses the program execution for the specified duration.exec(path, args, stdin): Executes a command specified bypathwith the givenargsand optional standard inputstdin. Returns stdout, stderr, and end events.
I/O Interfaces
input(): Reads input from the standard input.print(): Writes output to the standard output.ls(path): Lists files and directories in the specified path.mkdir(path): Creates a directory at the specified path.rm(path): Removes the file or directory at the specified path.stat(path): Retrieves file or directory information at the specified path.open(path, flags, perm): Opens a file at the specified path with the given flags and permissions.close(fd): Closes the file descriptorfd.read(fd, offset, length): Reads data from the file descriptorfdstarting at the specifiedoffsetand readinglengthbytes.write(fd, offset, data): Writes data to the file descriptorfdstarting at the specifiedoffset.close := listen(host, handler): Listens for incoming connections on the specifiedhostand handles them with the providedhandlerfunction.req(data): Sends an HTTP request with the provided data.
`go // Req syntax: // ---
req({ url: '' method: 'GET' headers: {} body: _ }) `
Math Functions
- Trigonometric functions
sin(n): Calculates the sine of the anglen.cos(n): Calculates the cosine of the anglen.tan(n): Calculates the tangent of the anglen.
- Inverse Trigonometric functions
asin(n): Calculates the arcsine of the valuen.acos(n): Calculates the arccosine of the valuen.atan(n): Calculates the arctangent of the valuen.
- Power and logarithmic functions
pow(b, n): Raises the basebto the power ofn.log(b, n): Calculates the logarithm ofnwith baseb.