-
Notifications
You must be signed in to change notification settings - Fork 0
/
language-syntax.em
92 lines (77 loc) · 1.96 KB
/
language-syntax.em
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
module main;
# Use with caution as this will make the type-checker thing these exist.
extern println (any) void;
extern print (any) void;
extern typeof (any) string;
extern equals (any,any) boolean;
extern as_str (any) string;
extern as_num (any) number;
extern as_bool (any) boolean;
extern exit (number) void;
# Demo of imports from std
import math;
import random;
import fs;
# importing other em code
import other from "../other";
import foo from "../lib/foo";
# Exporting some basic math functions
pub fn add (x number, y number) number { return x + y; }
pub fn sub (x number, y number) number { return x - y; }
pub fn mul (x number, y number) number { return x * y; }
pub fn div (x number, y number) number { return x / y; }
# Define a few types
type NumStr = number | string;
type Location = { x number, y number }
type Person = {
name string,
age number,
birthday fn ()void,
location: Location
}
# Control Flow
var rnd number = math.random();
if rnd < 100 {
println("less than 100");
} else if math.random() == 7 {
println("Lucky");
} else {
println("Quitting the program");
exit(1);
}
match typeof(rnd) {
"number" {
println("Number");
}
else {
println("This should have been a number!");
}
}
# Loops
for num, index in array.generate_numbers(100) {
println ("Current number is {} at index {}".format(number, index));
}
while time.hour() != 12 {
println("Still not noon!");
}
# Literals
# Creating objects is easy
var person Person = {
name: "Tyler",
age: 22,
birthday: fn () {
println("Happy birthday");
},
location: {
x: -12.03,
y: 10232.383
}
};
# so is creating array literals
mut numbers_and_strings []NumStr = ["Hello world", 21.33, "Foo", 32.4, -23, "Baz"];
numbers_and_strings.push(10);
numbers_and_strings.push("Baz");
# In Expressions
if "Foo" in numbers_and_strings {
println("Foo exists in array");
}