-
Notifications
You must be signed in to change notification settings - Fork 3
Documentation v2.7
Official documentation for the RAM programming language. Please open an issue if you find any bugs, or want some features to be changed or added.
A program should always start with a main:
label, defining a block. Blocks are like functions where you can jump anytime using jmp
or conditional jumps (see jump reference to learn how to use them). Each label contains a block which is executed when the label is called. The program has a stack which stores integers and there exists some commands to manipulate it.
The language has 6 general purpose variables (as in x86/arm), 4 global and 4 local dynamic named variables. Global variable's data can be changed and read from within other blocks.
-
lx
andrv
store floating point integers. -
string
andlxstring
to store strings. -
vec int
andvec str
to store integer and string vectors.
NOTE: These variables can be read from any block but cannot be modified without jumping back to the block in which the user wishes to modify the value of the variable.
These variables are called global_var. There can be unlimited number of these named variables, restricted to types like string, integer, integer vector and string vector. You can move values from these vars to general purpose vars for further operations.
NOTE: These variables are truly global. They can change from a block without calling any jump statements.
They are simply called var, and are readable and writable only in the block which declares them. But they can also be passed to other blocks/functions as parameters.
General Commands | Std commands | Stack commands | Vars | Operations |
---|---|---|---|---|
Comments | stdin | ram | vars | add, sub, div, mul |
stdfs | vectors | move | split | |
printc | random | str | str | |
halt | pop | sqr, sqrt | ||
popall | average | |||
round |
// this is a one line comment
-
print
prints the last number on the screen present in the stack. -
print lx/rv
prints the value oflx/rv
variables on the screen. -
print string/lxstring
prints the value ofstring/lxstring
variables on the screen. -
print vec str/vec int
prints the value ofvec str/vec int
variables on the screen. -
print global_var <var> str
prints the value ofstr_var
variable on the screen. -
print global_var <var> int
prints the value ofint_var
variable on the screen. -
print global_var <var> str vec
prints the value ofstr_vec_var
variable on the screen. -
print global_var <var> int vec
prints the value ofint_vec_var
variable on the screen. -
print var <var> str
prints the value ofstr_var
variable on the screen. -
print var <var> int
prints the value ofint_var
variable on the screen. -
print var <var> str vec
prints the value ofstr_vec_var
variable on the screen. -
print var <var> int vec
prints the value ofint_vec_var
variable on the screen.
Prints the characters specified on the screen
halt
stops the program
stdin lx/rv/string
sets the value of the standard input provided in the next line to the specified variable.
-
stdfs open >> ./path/to/file
opens the file and stores the contents as strings in thestring
variable. -
stdfs open string/lxstring
opens the file from string/lxstring and stores the contents as strings in thestring
variable.
rand >> num1,num2/lx,rv/rv,lx
generates a random decimal between num1 and num2 (including both) and pushes it to stack. May round
be used after this statement to round it to an integer.
parse int lx/rv string/lxstring
parses string/lxstring and stores in lx/rv.
-
ram <number>
adds the number to the stack. -
ram lx/rv <number>
assigns the number to the variable -
ram lx/rv
adds the variable value to the stack for further calculations -
ram lx/rv prev
assigns the last stack value to the specified variable. -
ram string >> <strings>
assigns value to the strings global variable. -
ram global_var <name>
adds the int global_var value to the stack. -
ram global_var <name> prev
assigns the last stack value to the specified global int variable. -
ram var <name>
adds the int var value to the stack. -
ram var <name> prev
assigns the last stack value to the specified var int variable.
-
ram vec int >> [1,2,3,4,ints...]
creates a vector and assigns it to the globalvec int
variable. -
ram vec str >> [1,2,3,4,strs...]
creates a vector and assigns it to the globalvec str
variable. -
vec str push
pushes the value ofstring
tovec str
variable. -
vec int push lx/rv
pushes the value oflx/rv
tovec int
variable. -
vec str >> [<index>/lx/rv]
extracts value of the index position ofvec str
and stores it in thestring
variable. -
vec int lx/rv >> [<index>/lx/rv]
extracts the value of the index position ofvec int
and stores it inlx/rv
variable. -
vec str len
pushes the length of thevec str
variable to the stack. -
vec int len
pushes the length of thevec int
variable to the stack. -
vec int pop
pops the last item from thevec int
. -
vec str pop
pops the last item from thevec str
.
split >> "<arg>"
splits the string
variable into args and stores the values into the vec str
-
str lxstring string
moves the value of thestring
variable tolxstring
. -
str string lxstring
moves the value of thelxstring
variable tostring
. -
str cmp
compares the variableslxstring & strings
and pushes 0 to the stack if they are equal, and -1 if they are not.
-
pop
Removes the last value from the stack -
pop <n>
Removes the lastn
values from the stack
Empties the entire stack. Useful for avg
implementation.
-
var <name> str >> test123
creates a variable calledname
with string type and assigns "test123" to it. -
var <name> int >> 75
creates a variable calledname
with integer type and assigns 75 to it. -
var <name_vec> int vec >> [10, 13, 12]
creates a variable calledname_vec
with vec integer type and assigns [10, 13, 12] to it. -
var <name_vec> str vec >> [one,two]
creates a variable calledname_vec
with vec integer type and assigns [one,two] to it. -
var <name_vec> str vec push >> string/lxstring/var <name>
pushes string/lxstring/<name>
into the<name_vec>
variable. -
var <name_vec> int vec push >> lx/rv/var <name>
pushes lx/rv/<name>
into the<name_vec>
variable. -
var <name_vec> int vec lx/rv/var <name> >> [lx/rv/var name/<number>]
stores the [index] value of<name_vec>
into the specified variable. -
var <name_vec> str vec string/lxstring/var <name> >> [lx/rv/var name/<number>]
stores the [index] value of<name_vec>
into the specified variable. -
var <name_vec> str vec len
-> pushes the length of str<name_vec>
into the stack. -
var <name_vec> int vec len
-> pushes the length of int<name_vec>
into the stack.
-
global_var <name> str >> test123
creates a variable calledname
with string type and assigns "test123" to it. -
global_var <name> int >> 75
creates a variable calledname
with integer type and assigns 75 to it. -
global_var <name_vec> int vec >> [10, 13, 12]
creates a variable calledname_vec
with vec integer type and assigns [10, 13, 12] to it. -
global_var <name_vec> str vec >> [one,two]
creates a variable calledname_vec
with vec integer type and assigns [one,two] to it. -
global_var <name_vec> str vec push >> string/lxstring/var <name>
pushes string/lxstring/<name>
into the<name_vec>
variable. -
global_var <name_vec> int vec push >> lx/rv/var <name>
pushes lx/rv/<name>
into the<name_vec>
variable. -
global_var <name_vec> int vec lx/rv/var <name> >> [lx/rv/var name/<number>]
stores the [index] value of<name_vec>
into the specified variable. -
global_var <name_vec> str vec string/lxstring/var <name> >> [lx/rv/var name/<number>]
stores the [index] value of<name_vec>
into the specified variable. -
global_var <name_vec> str vec len
-> pushes the length of str<name_vec>
into the stack. -
global_var <name_vec> int vec len
-> pushes the length of int<name_vec>
into the stack.
Move is used to move the value of vars to general purpose vars and vice versa (note its destination first).
-
move int lx/rv = var <name>
moves the value ofname
variable tolx/rv
(make surename
is of int type) -
move str string/lxstring = var <name>
moves the value ofname
variable tostring/lxstring
(make surename
is of str type) -
move str var <name> = string/lxstring
moves the value ofstring/lxstring
toname
var (make surename
is of str type) -
move int var <name> = lx/rv
moves the value oflx/rv
toname
var (make surename
is of int type) -
move vec vec str = var <name>
moves the value ofname
var tovec str
(make surename
is a string vector) -
move vec vec int = var <name>
moves the value ofname
var tovec int
(make surename
is a integer vector) -
move vec var <name> = vec int
moves the value ofvec int
toname
var (make surename
is a integer vector) -
move vec var <name> = vec str
moves the value ofvec str
toname
var (make surename
is a string vector)
- adds last two numbers in the stack and pushes the result to the stack
-
add lx rv
adds the two variables and pushes the result to the stack
subtracts the last number from the number added prior to it and pushes to stack
-
mul
multiplies the last two numbers in the stack and adds the result to the stack -
mul rv lx
multiplies lx and rv and pushes the result to the stack.
the number added prior to the last number / last number and pushes to stack.
-
sqr
squares the last number in the stack and adds the result to the stack. -
sqr lx/rv
squareslx/rv
and assigns the result to the variable squared.
-
sqrt
squares the last number in the stack and adds the result to the stack. -
sqrt lx/rv
squareslx/rv
and assigns the result to the variable squared.
Takes out the average of all the numbers present in the stack. popall
should be used somewhere in the code to remove the vars which are redundant.
-
round
rounds the last number present in the stack to decimals and pushes it to the stack. -
round lx/rv
rounds the specified variable and assigns the result to the var itself.
The program is devided into labels and we can jump on any label to execute the code block below it.
main:
ram 100
ram 300
cmp
jne not_equal:
cmp
je equal:
not_equal:
printc >> Not Equal
equal:
printc >> Equal
The main:
label is considered compulsory at the beginning of the program.
-
jmp <label:>
- jumps to the label -
cmp
pushes -1, 0, 1 to the stack if the previous number smaller, equal or greater than the number after it in the stack.
main:
ram 10
ram 20
cmp
print
// this will print -1 as 10 < 20
-
jne <label:>
- jumps to the label if the previous cmp statement is not 0 (not equal) -
je <label:>
- jumps to the label if the previous cmp statement is 0 (equal) -
jgr <label:>
- jumps to the label if the prev cmp statement is 1 (greater) -
jsm <label:>
- jumps to the label if the prev cmp statement is -1 (smaller)
Imports are restricted only to the main file. You can import as many files you want using include <filepath>
and then you shall be able to call all the codeblocks associated with the file.
// main.ram
include "ok.ram"
main:
printc >> hello from main
jmp test:
// ok.ram
test:
printc >> hello from ok.ram
main:
printc >> Coin Flip
rand >> 0,1
round
ram 1
cmp
je heads:
pop
ram 0
cmp
je tails:
heads:
printc >> Heads
tails:
printc >> Tails