Skip to content

Commit 0994799

Browse files
committed
Example: Introduce all expressions
1 parent 7526e82 commit 0994799

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

examples/07-expressions.frank

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# The previous examples introduced objects, freezing, regions and cowns.
2+
# This example is a showcase of most expressions that FrankenScript
3+
# supports. It can be used as a reference for writing your own scripts.
4+
#
5+
# A list of built-in function can be found in the `docs` folder. Here is
6+
# a link to the rendered version on GitHub:
7+
# <https://github.com/fxpl/frankenscript/blob/main/docs/builtin.md>
8+
9+
# This is how you construct the objects discussed in example 01
10+
a = {}
11+
x = "a new string"
12+
x = Region()
13+
x = Cown(None)
14+
15+
# FrankenScript has a few built-in and frozen objects:
16+
x = True # The boolean value `true`
17+
x = False # The boolean value `false`
18+
x = None # The null value, similar to Python's None
19+
20+
# FrankenScript supports functions with arguments and return values.
21+
def id(x):
22+
return x
23+
24+
# A function can be called like this. Each function call adds a new frame
25+
# to the diagram. The frame holds all variables known to the current scope.
26+
# The frame is deleted when the function returns.
27+
id(a)
28+
29+
# Function objects are hidden from the mermaid to make it cleaner. But they're
30+
# still normal objects that can be used in assignments like this:
31+
a.id = id
32+
33+
# This can be used to simulate method calls. Calling a function on stored in a
34+
# field will pass the object in as the first argument.
35+
a = a.id() # a is passed in as the first argument
36+
37+
# The move keyword can be used for destructive reads. The previous location
38+
# is reassigned to None.
39+
b = move a
40+
41+
# FrankenScript has two comparison operators. These check for object identity:
42+
res = b == None
43+
res = b != None
44+
45+
# Boolean values can be used in if statements:
46+
if res:
47+
pass() # A built-in function for empty blocks
48+
else:
49+
unreachable() # A built-in function for unreachable branches
50+
51+
# The else block is optional:
52+
if res:
53+
pass()
54+
55+
# Boolean expressions can also be used in while loops:
56+
while res == True:
57+
res = False
58+
59+
# For loops can be used to iterate over all fields of an object.
60+
a = {}
61+
a.field = "value"
62+
for key, value in b:
63+
pass()

src/rt/core.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,10 @@ namespace rt::core
166166
std::string value;
167167

168168
public:
169-
StringObject(std::string value_)
170-
: objects::DynObject(stringPrototypeObject()), value(value_)
169+
StringObject(
170+
std::string value_,
171+
objects::Region* region = rt::objects::get_local_region())
172+
: objects::DynObject(stringPrototypeObject(), region), value(value_)
171173
{}
172174

173175
std::string get_name() override
@@ -190,13 +192,15 @@ namespace rt::core
190192

191193
inline StringObject* trueObject()
192194
{
193-
static StringObject* val = new StringObject("True");
195+
static StringObject* val =
196+
new StringObject("True", objects::immutable_region);
194197
return val;
195198
}
196199

197200
inline StringObject* falseObject()
198201
{
199-
static StringObject* val = new StringObject("False");
202+
static StringObject* val =
203+
new StringObject("False", objects::immutable_region);
200204
return val;
201205
}
202206

0 commit comments

Comments
 (0)