2023-08-04 08:37:37 +08:00
|
|
|
//! Simple Lua interpreter
|
|
|
|
//! This is a modified program from Programming in Lua 4th Edition
|
|
|
|
|
|
|
|
const std = @import("std");
|
2023-08-05 05:47:15 +08:00
|
|
|
|
|
|
|
// The ziglua module is made available in build.zig
|
2023-08-04 08:37:37 +08:00
|
|
|
const ziglua = @import("ziglua");
|
|
|
|
|
|
|
|
pub fn main() anyerror!void {
|
|
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
|
|
const allocator = gpa.allocator();
|
|
|
|
defer _ = gpa.deinit();
|
|
|
|
|
|
|
|
// Initialize The Lua vm and get a reference to the main thread
|
|
|
|
var lua = try ziglua.Lua.init(allocator);
|
|
|
|
defer lua.deinit();
|
|
|
|
|
2023-08-05 05:47:15 +08:00
|
|
|
// Open all Lua standard libraries
|
2023-08-04 08:37:37 +08:00
|
|
|
lua.openLibs();
|
|
|
|
|
|
|
|
var stdin = std.io.getStdIn().reader();
|
|
|
|
var stdout = std.io.getStdOut().writer();
|
|
|
|
|
|
|
|
var buffer: [256]u8 = undefined;
|
|
|
|
while (true) {
|
|
|
|
_ = try stdout.write("> ");
|
|
|
|
|
|
|
|
// Read a line of input
|
|
|
|
const len = try stdin.read(&buffer);
|
|
|
|
if (len == 0) break; // EOF
|
|
|
|
if (len >= buffer.len - 1) {
|
|
|
|
try stdout.print("error: line too long!\n", .{});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the buffer is null-terminated so the Lua API can read the length
|
|
|
|
buffer[len] = 0;
|
|
|
|
|
|
|
|
// Compile a line of Lua code
|
|
|
|
lua.loadString(buffer[0..len :0]) catch {
|
2023-08-05 05:47:15 +08:00
|
|
|
// If there was an error, Lua will place an error string on the top of the stack.
|
|
|
|
// Here we print out the string to inform the user of the issue.
|
2023-08-04 08:37:37 +08:00
|
|
|
try stdout.print("{s}\n", .{lua.toString(-1) catch unreachable});
|
2023-08-05 05:47:15 +08:00
|
|
|
|
|
|
|
// Remove the error from the stack and go back to the prompt
|
2023-08-04 08:37:37 +08:00
|
|
|
lua.pop(1);
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Execute a line of Lua code
|
|
|
|
lua.protectedCall(0, 0, 0) catch {
|
2023-08-05 05:47:15 +08:00
|
|
|
// Error handling here is the same as above.
|
2023-08-04 08:37:37 +08:00
|
|
|
try stdout.print("{s}\n", .{lua.toString(-1) catch unreachable});
|
|
|
|
lua.pop(1);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|