Skip to content
/ toylang Public

My first toy language. Created exclusively for learning purposes.

License

Notifications You must be signed in to change notification settings

dptole/toylang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Toylang

Build status Issue status NPM Version Downloads Say thanks

My first toy language. Created exclusively for learning purposes.

Example

const toylang = require('@dptole/toylang')
const ast = toylang.syntax.parse(`

user-name = 'dptole'
user-name-length = length(user-name)

if(0 < user-name-length < 10) {
  print('valid username length. its length is between 1 and 9')
  print(user-name)
} else {
  print('invalid username')
}

`)

const result = toylang.interpreter.parse(ast)
// It will output
// "valid username length. its length is between 1 and 9"
// "dptole"

Click here to see more.

ABNF-like syntax

chunk = [ exp | decl ] *

exp = [ assign | math_operation | func_call | func_def | primitive | variable ] [ ext_exp ] *

assign = variable "=" exp

variable = [ a-z ] + [ [ "-" ] ? [ _a-z0-9 ] + ] *

math_operation = exp [ math_operator exp ] +

math_operator = [ "-" | "+" | "*" | "/" ]

func_call = variable func_call_args_chunk

func_call_args_chunk = "(" [ func_call_args_list ] ? ")"

func_call_args_list = exp [ "," exp ] *

func_def = "f " variable "(" [ func_def_args_list ] ? ")" "{" func_def_chunk "}"

func_def_args_list = variable [ "," variable ] *

func_def_chunk = chunk func_def_return

func_def_return = "return " exp

primitive = [ number | string | boolean | array | object ]

number = [ "+" | "-" ] ? [ 0-9 ] + [ "." [ 0-9 ] + ] ?

string = [ "'" [ ALPHA ] + "'" ] | [ """ [ ALPHA ] + """ ]

boolean = [ "T" | "F" ]

array = "[" [ exp [ "," exp ] * ] ? "]"

object = "{" [ variable "=" exp [ ";" variable "=" exp ] * ] ? "}"

ext_exp = [ ext_object | ext_array | func_call_args_chunk ]

ext_object = "." exp

ext_array = "[" exp "]"

decl = if

if = "if" if_cond_block if_chunk_block [ else ] ?

if_cond_block = "(" cond ")"

cond = [ log_unary ] ? exp [ log_op exp ] *

log_unary = "not"

log_op = ">" | "<" | "==" | "!=" | ">=" | "<=" | "and" | "xor" | "or"

if_chunk_block = "{" chunk "}"

else = [ else_middle ] * else_end

else_middle = "else" if_cond_block if_chunk_block

else_end = "else" if_chunk_block

License

MIT

Releases

No releases published

Packages

No packages published