reality-engine/docs/SPECIFICATION.org

480 lines
8.5 KiB
Org Mode
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

* /ZRL/ (Reality Engine Language) Design parameters
:PROPERTIES:
:CUSTOM_ID: zrl-zongors-reality-engine-language-design-parameters
:END:
** What is /zrl/?
:PROPERTIES:
:CUSTOM_ID: what-is-zrl
:END:
/zrl/ is an domain specific language for 3D games with C/Lua style syntax.
The compiler is written in C which should make it easy to port to other
systems.
* /ZRL/ Grammar and Specification
:PROPERTIES:
:CUSTOM_ID: zrl-grammar-and-specification
:END:
** Types
:PROPERTIES:
:CUSTOM_ID: types
:END:
- there are also a list of "substantial types" which come with the
language which are the building blocks for more complex types. If you
are coming from object oriented languages you can think of self as
"primitive types"
- Note that these look like classes but act like structs
the methods actually have a implied struct as their first argument
#+begin_src zrl
type «token» {
init() {
// values
}
}
! example
type Vec3 {
init(x real, y real, z real) {
this.x = x;
this.y = z;
this.y = z;
}
}
#+end_src zrl
* Basic Types
:PROPERTIES:
:CUSTOM_ID: substantial-types
:END:
** numeric
:PROPERTIES:
:CUSTOM_ID: numeric
:END:
- =real=
- 32 bit floats
- =int=
- 32 bit integer
- =nat=
- 32 bit unsigned integer (for loop counting and indexing)
** string
:PROPERTIES:
:CUSTOM_ID: string
:END:
- =str=
- utf8 / ascii encoded string depending on the language output
normal string
="«utf8 encoded characters»"=
string interpolation
="«utf8 encoded characters» ${some_var}"=
** binary
:PROPERTIES:
:CUSTOM_ID: binary
:END:
- =byte=
- same as uint8 or c char, also used for interop
** logical
:PROPERTIES:
:CUSTOM_ID: logical
:END:
- =bool=
- =true= / =false=
** datastructure
:PROPERTIES:
:CUSTOM_ID: datastructure
:END:
*** Array
:PROPERTIES:
:CUSTOM_ID: array
:END:
Array of a specific type
#+begin_src zrl
«type»[«length»] «variable» = [val1, val2, ...];
#+end_src zrl
*** Tunnel
:PROPERTIES:
:CUSTOM_ID: tunnel
:END:
described in "tunnel" section
*** Basic operators
:PROPERTIES:
:CUSTOM_ID: basic-operators
:END:
The following is a list of global operators and their effect:
- !
- comment
- ??
- unwrap or
- .?
- null check or return error
- +
- addition
- -
- subtraction
- negation
- *
- multiplication
- /
- divisor
- ^
- power
- ==
- equals
- <
- less than
- >
- greater than
- >=
- greater than or equals
- <=
- less than or equals
- .
- accessor
- ++
- inline add 1
- --
- inline subtract 1
- +=
- inline add n
- -=
- inline subtract n
- *=
- inline multiply n
- \=
- inline divide n
*** logical / bitwise operators
:PROPERTIES:
:CUSTOM_ID: logical-bitwise-operators
:END:
- =mod=
- modulo
- =not=
- logical not
- =and=
- logical and
- =or=
- logical or
- =xor=
- logical xor
- =band=
- bitwise and
- =bor=
- bitwise or
- =bxor=
- bitwise xor
- =srl=
- bit shift right
- =sll=
- bit shift left
*** keywords
:PROPERTIES:
:CUSTOM_ID: keywords
:END:
=let=
let operator
#+begin_src zrl
let «token» = true;
#+end_src zrl
=is=
checks if a object is of that type
#+begin_src zrl
if («token» is real) {
print("hello yes self is a real?");
}
#+end_src zrl
also used for letting constants
=as=
coerces a type as another type if possible
#+begin_src zrl
let «token» = 0; ! default is int
some_functon(«token» as real); ! needs a real
#+end_src zrl
=in=
checks if a object's type, or a type implements a contract
#+begin_src zrl
if («token» in Tunnel, Drawable) {
print("im tunnel-able and draw-able");
}
#+end_src zrl
also used inside of the for loops
#+begin_src zrl
for («token» in «collection») { «body» }
#+end_src zrl
** Object
:PROPERTIES:
:CUSTOM_ID: object
:END:
An object is an invoked type.
#+begin_src zrl
let «variable» = «type»(«fields», …);
#+end_src zrl
** Tunnel
:PROPERTIES:
:CUSTOM_ID: tunnel-1
:END:
Represents a path to a file, url endpoint, other process endpoint (like
a socket, etc.)
Tunnels are inspired by translators in gnu/hurd, plan9 9p protocol, and
unix sockets
tunnels are invoked like objects, but have scope like control flow end
scope closes the tunnel
note the type must always be of a type which is "tunnel-able"
i.e. Files, sockets, etc
Tunnels have almost the same interface as 9p since they are closely
based on 9p.
*** transtypes for tunnels
:PROPERTIES:
:CUSTOM_ID: transtypes-for-tunnels
:END:
=tunnel? : attach(tunnel_object)= -> open communication
=success? : tunnel_object.clunk()= -> close communication
=success? : tunnel_object.flush()= -> cancels long operation and dumps
whatever is in buffer
=success? : tunnel_object.open(resource, mode)= -> opens a tunnel for
doing operations on
=success? : tunnel_object.create(resource)= -> creates the object from
the database graph/file from file structure
=data? : tunnel_object.read(resource)= -> reads from a tunnel
=success? : tunnel_object.write(resource, data)= -> writes to a tunnel
=success? : tunnel_object.remove(resource)= -> removes the object from
the database graph/file from file structure
=stat_data? : tunnel_object.stat(resource)= -> returns the status of the
file/resource
=version? : tunnel_object.version()= -> returns the version code for the
connected tunnel
=success? : tunnel_object.walk(path_or_endpoint)= -> moves around the
filesystem or through the graph
#+begin_src zrl
! client
let endpoint = Client("tcp://path/to/source");
let tunnel = endpoint.attach(user, auth);
let data = tunnel.open("/some/resource").read();
std.write(data);
data.flush();
endpoint.clunk();
! server
let server = Server("tcp://0.0.0.0:25565");
s.bind("/some/resource", fn () str {
return "hello world";
})
server.start();
#+end_src zrl
** Functions
:PROPERTIES:
:CUSTOM_ID: functions
:END:
Functions are all typechecked statically at compile time. Since we
always have a "default type" for all constant values or a developer can
use the =as= keyword we do not have to define all values like in C,
while keeping the same type safety as a more strongly typed language.
#+begin_src zrl
fn «token» («parameter» «type», ...) «return_type» {
«body»
}
#+end_src zrl
- Built in transtypes
- sort
- filter
- trig functions
- calc functions
- statistical functions
** Control flow
:PROPERTIES:
:CUSTOM_ID: control-flow
:END:
*** loops
:PROPERTIES:
:CUSTOM_ID: loops
:END:
#+begin_src zrl
for («variable» in «collection») { «body» }
#+end_src zrl
iterates through each object in the collection setting it to variable
#+begin_src zrl
while («boolean expression») { «body» }
#+end_src zrl
loops until the expression is false
#+begin_src zrl
do («variable» = initial_value, end_value, increment) { «body» }
#+end_src zrl
loops from initial value to end value by increment value (like a for loop in other languages)
*** branching
:PROPERTIES:
:CUSTOM_ID: branching
:END:
#+begin_src zrl
if («boolean expression») {
} else if («boolean expression») {
} else {
}
#+end_src zrl
#+begin_src zrl
switch (value) {
case A:
case B:
case C:
default:
}
#+end_src zrl
** Error handling
:PROPERTIES:
:CUSTOM_ID: error
:END:
Error handling is much like in C/C++ where a try catch can be used.
#+begin_src zrl
let rr = nil;
let var = rr ?? 0; ! value is 0
try {
let other_var = 1 / rr; ! will panic
} catch (e) {
print("Caught error ${e}");
}
#+end_src zrl
** Localization
:PROPERTIES:
:CUSTOM_ID: localization
:END:
will look up the text of «token» in the linked localization.json file
#+begin_src zrl
#«token»
#+end_src zrl
#+begin_src json
{
"some_token": [
"localization_1": ""
],
"some_other_token": [
"localization_1": "",
"localization_2": ""
]
}
#+end_src
** Libraries and "includes"
:PROPERTIES:
:CUSTOM_ID: libraries-and-includes
:END:
In most languages the include or use statements get libraries which link
to other files and so on.
#+begin_src zrl
use "./some_local_file.zrl"
#+end_src zrl
** Testing
:PROPERTIES:
:CUSTOM_ID: testing
:END:
*** assertion
:PROPERTIES:
:CUSTOM_ID: assertion
:END:
#+begin_src zrl
assert(«expression», «expected output») ! returns «error or none»
#+end_src zrl
** Measurements
:PROPERTIES:
:CUSTOM_ID: measurements
:END:
- types
- time
- unit
- seconds (s)
- subtypes
- date
- Default is ISO 8601
- length
- unit
- metre (m)
- subtypes
- angle
- radian (rad)
- mass
- unit
- kilogram (kg)
- electric current
- unit
- ampere (a)
- temperature
- unit
- kelvin (K)
- amount of substance
- unit
- mol (mol)
- luminous intensity
- unit
- candela (candela)