-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added examples that can be run with
zig build example -Dexample
- Loading branch information
1 parent
edf11c0
commit 530342c
Showing
12 changed files
with
168 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.zig text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const x = 15 | ||
const y = 20 | ||
|
||
mut z = x + y | ||
z = z << 1 | ||
|
||
|
||
//this allows example to print last value | ||
z |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const MyEnum = enum{ | ||
first_enum, | ||
second_enum, | ||
third_enum | ||
} | ||
|
||
//this allows example to print last value | ||
MyEnum.third_enum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const std = @import("std"); | ||
const luf = @import("luf"); | ||
|
||
const example = @import("build_options").example; | ||
|
||
pub fn main() !void { | ||
if (example == null) | ||
return std.debug.print( | ||
"No example was provided, use -Dexample=example_name to run an example", | ||
.{}, | ||
); | ||
|
||
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
defer _ = gpa.deinit(); | ||
|
||
const allocator = &gpa.allocator; | ||
|
||
const example_path = try std.fs.path.join(allocator, &[_][]const u8{ | ||
"examples", | ||
example.?, | ||
}); | ||
defer allocator.free(example_path); | ||
|
||
const example_file = try std.mem.concat(allocator, u8, &[_][]const u8{ | ||
example_path, | ||
".luf", | ||
}); | ||
defer allocator.free(example_file); | ||
|
||
const file = std.fs.cwd().openFile(example_file, .{}) catch |_| { | ||
return std.debug.print("Example does not exist", .{}); | ||
}; | ||
defer file.close(); | ||
|
||
const size = try file.getEndPos(); | ||
|
||
const source = try file.readAllAlloc(allocator, size, size); | ||
defer allocator.free(source); | ||
|
||
const writer = std.io.getStdErr().writer(); | ||
var vm = luf.Vm.init(allocator); | ||
defer vm.deinit(); | ||
vm.compileAndRun(source) catch |err| { | ||
try vm.errors.write(source, writer); | ||
return err; | ||
}; | ||
|
||
try vm.peek().print(writer); | ||
try writer.writeAll("\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const sum = fn(a:int, b:int) int { | ||
return a + b | ||
} | ||
|
||
const x: int = sum(5, 6) | ||
|
||
//this allows example to print last value | ||
x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
mut x = 5 | ||
|
||
if (x == 4) { | ||
x = 2 | ||
} else if (x == 3){ | ||
x = 1 | ||
} else { | ||
x = 0 | ||
} | ||
|
||
//this allows example to print last value | ||
x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const list = []int{1,2,3} | ||
const map = []string:int{"hello":1, "world":2} | ||
|
||
const sum = map["hello"] + map["world"] | ||
|
||
//this allows example to print last value | ||
sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const list = []int{1,2,3} | ||
mut sum = 0 | ||
for(list) |element, index| { | ||
sum += element + index | ||
} | ||
|
||
for(20..50) |i| { | ||
sum += i | ||
} | ||
|
||
mut i = 0 | ||
while(i<10) { | ||
sum += i | ||
i+=1 | ||
} | ||
|
||
//this allows example to print last value | ||
sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters