Language parsing 01-01-2017, 10:49 PM
#1
So, I got bored the other day and decided to write a descent parser for a made up programming language. The language structure is as follows:
variables:
variables are dynamic and are created on initialization, the parser will throw an error if they are used before then. Variable names have no rules except that they MUST be 31 characters long or less, and must not include the characters +-/*$= (it will let you assign these, but they won't be usable)
Assigning a variable can follow any of these formats:
the order (var + 5 or 5 + var) is not important
Arithmatic operations
add (+)
subtract (-)
multiply (*)
divide (/)
modulo (%)
Special features:
printing variables to screen
there exists two specials for printing variables:
prints all variables in list to screen
prints the variable "name" to screen
deleting variables
calling functions
There are two ways to call functions, always and conditionally.
will load the file procname, copy passed variables and execute until return
will load procname, copy passed variables, and execute until return if and only if condition evaluates to zero
Returning from a procedure
When executed in the main context will end the program, when executed in the context of a subprocedure will copy back variables, clean up the list, and return
Reading input
will read a value from user and store it in variable named name
Printing output
this is self explanatory.
Notes on calling functions:
variables are passed by value, and utilize a prefix. Only variables prefixed with the function name and a dot will be passed in or out. Example:
function m will only have access to the variables m.a and m,i with values 5 and 8 respectively.
Sample program:
sample
stuff
mathops
![[Image: 1fYMsVy.png]](http://i.imgur.com/1fYMsVy.png)
You can find the source code for this here. If you are interested, I may write a tutorial on language parsing and creating your own language.
PLEASE give feedback!
variables:
variables are dynamic and are created on initialization, the parser will throw an error if they are used before then. Variable names have no rules except that they MUST be 31 characters long or less, and must not include the characters +-/*$= (it will let you assign these, but they won't be usable)
Assigning a variable can follow any of these formats:
Code:
name = <constant>
name = <variable>
name = <variable> +-*/% <variable>
name = <variable> +-*/% <constant>
Arithmatic operations
add (+)
subtract (-)
multiply (*)
divide (/)
modulo (%)
Special features:
printing variables to screen
there exists two specials for printing variables:
Code:
.print
Code:
.print(name)
deleting variables
Code:
.delete(name)
calling functions
There are two ways to call functions, always and conditionally.
Code:
.call(procname)
Code:
.ifcall(procname, condition)
Returning from a procedure
Code:
.done
Reading input
Code:
.input(name)
Printing output
Code:
.echo(text to output)
Notes on calling functions:
variables are passed by value, and utilize a prefix. Only variables prefixed with the function name and a dot will be passed in or out. Example:
Code:
a = 5
i = 7
m.a = a
m.i = i
m.i = m.i + 1
.call(m)
Sample program:
sample
Code:
.echo(Do you want to execute program? 1/0)
.input(firstexec)
.ifcall(stuff, firstexec - 1)
.echo(Running proc stuff, this may be the second time)
.call(stuff)
.done
stuff
Code:
.echo(Enter first number)
.input(m.a)
.echo(Enter second number)
.input(m.b)
.call(m)
add = m.add
sub = m.sub
mul = m.mul
div = m.div
mod = m.mod
.print(add)
.print(sub)
.print(mul)
.print(div)
.print(mod)
.done
mathops
Code:
a = m.a
b = m.b
m.add = a + b
m.sub = a - b
m.mul = a * b
m.div = a / b
m.mod = a % b
.done
![[Image: 1fYMsVy.png]](http://i.imgur.com/1fYMsVy.png)
You can find the source code for this here. If you are interested, I may write a tutorial on language parsing and creating your own language.
PLEASE give feedback!