aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-01-31 01:26:07 -0500
committerMustafa Quraish <[email protected]>2022-01-31 01:26:07 -0500
commitcb0d77c3dc83d6c7cb39642e8708b12a29b7b8c7 (patch)
tree904a6c70509da3f9274bb2df8e214f2b9e6fd9ac /examples
parentPut tokens in their own macro to allow looping over them (diff)
downloadcup-cb0d77c3dc83d6c7cb39642e8708b12a29b7b8c7.tar.xz
cup-cb0d77c3dc83d6c7cb39642e8708b12a29b7b8c7.zip
Add basic builtin-function support
This isn't really super extendible for now, but it's a start and gives us the `print` builtin which allows us to finally actually print out values to the screen, so we can move away from testing with exit codes eventually.
Diffstat (limited to 'examples')
-rw-r--r--examples/fibonacci.cup14
-rw-r--r--examples/hello.cup17
-rw-r--r--examples/variables.cup6
3 files changed, 34 insertions, 3 deletions
diff --git a/examples/fibonacci.cup b/examples/fibonacci.cup
new file mode 100644
index 0000000..5bf02aa
--- /dev/null
+++ b/examples/fibonacci.cup
@@ -0,0 +1,14 @@
+
+
+fn main() {
+ let a: int = 0;
+ let b: int = 1;
+ let N: int = 20;
+ let i: int ;
+ for (i = 0; i < 20; i = i+1) {
+ print(a);
+ let t: int = a;
+ a = b;
+ b = t + b;
+ }
+} \ No newline at end of file
diff --git a/examples/hello.cup b/examples/hello.cup
new file mode 100644
index 0000000..eef63eb
--- /dev/null
+++ b/examples/hello.cup
@@ -0,0 +1,17 @@
+
+fn main(): int {
+ putc(72);
+ putc(101);
+ putc(108);
+ putc(108);
+ putc(111);
+ putc(44);
+ putc(32);
+ putc(87);
+ putc(111);
+ putc(114);
+ putc(108);
+ putc(100);
+ putc(33);
+ putc(10);
+}
diff --git a/examples/variables.cup b/examples/variables.cup
index 60a7d6a..83f4982 100644
--- a/examples/variables.cup
+++ b/examples/variables.cup
@@ -1,7 +1,7 @@
fn main(): int {
- let x: int;
- let y: int;
- return x;
+ let x: int = 5;
+ let y: int = 6;
+ return x + y;
}