2019-12-08 23:07:41 +03:00
|
|
|
const std = @import("std");
|
|
|
|
const warn = @import("std").debug.warn;
|
|
|
|
|
|
|
|
const reader = @import("reader.zig");
|
|
|
|
const pcre = reader.pcre;
|
|
|
|
const printer = @import("printer.zig");
|
|
|
|
const getline = @import("readline.zig").getline;
|
|
|
|
const string_copy = @import("utils.zig").string_copy;
|
|
|
|
const string_concat = @import("utils.zig").string_concat;
|
|
|
|
const apply_function = @import("types.zig").apply_function;
|
|
|
|
const linked_list = @import("linked_list.zig");
|
|
|
|
const hash_map = @import("hmap.zig");
|
|
|
|
|
|
|
|
const Allocator = @import("std").heap.c_allocator;
|
|
|
|
|
|
|
|
const MalType = @import("types.zig").MalType;
|
|
|
|
const MalData = @import("types.zig").MalData;
|
|
|
|
const MalError = @import("error.zig").MalError;
|
|
|
|
const MalLinkedList = @import("linked_list.zig").MalLinkedList;
|
2019-12-19 08:07:11 +03:00
|
|
|
const MalHashMap = hash_map.MalHashMap;
|
2019-12-08 23:07:41 +03:00
|
|
|
|
2019-12-19 08:07:11 +03:00
|
|
|
var repl_environment: *MalHashMap = undefined;
|
2019-12-08 23:07:41 +03:00
|
|
|
|
2019-12-15 00:39:03 +03:00
|
|
|
fn READ(a: [] u8) MalError!?*MalType {
|
2019-12-08 23:07:41 +03:00
|
|
|
var read = try reader.read_str(a);
|
|
|
|
var optional_mal = reader.read_form(&read);
|
|
|
|
return optional_mal;
|
|
|
|
}
|
|
|
|
|
2019-12-19 08:07:11 +03:00
|
|
|
fn EVAL(mal: *MalType) MalError!*MalType {
|
2019-12-08 23:07:41 +03:00
|
|
|
switch(mal.data) {
|
|
|
|
.List => |ll| {
|
|
|
|
if(ll.len == 0) {
|
|
|
|
return mal;
|
|
|
|
}
|
2019-12-19 08:07:11 +03:00
|
|
|
var new_list = try eval_ast(mal);
|
2019-12-08 23:07:41 +03:00
|
|
|
return apply_function(Allocator, (try new_list.sequence_linked_list()).*);
|
|
|
|
},
|
|
|
|
else => {
|
2019-12-19 08:07:11 +03:00
|
|
|
return eval_ast(mal);
|
2019-12-08 23:07:41 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn PRINT(optional_mal: ?*MalType) MalError![] u8 {
|
|
|
|
return printer.print_str(optional_mal);
|
|
|
|
}
|
|
|
|
|
2019-12-19 08:07:11 +03:00
|
|
|
fn rep(input: [] u8) MalError!?[] u8 {
|
2019-12-15 00:39:03 +03:00
|
|
|
var read_input = (try READ(input)) orelse return null;
|
2019-12-19 08:07:11 +03:00
|
|
|
var eval_input = try EVAL(read_input);
|
2019-12-08 23:07:41 +03:00
|
|
|
var print_input = try PRINT(eval_input);
|
|
|
|
eval_input.delete(Allocator);
|
|
|
|
return print_input;
|
|
|
|
}
|
|
|
|
|
2019-12-19 08:07:11 +03:00
|
|
|
fn lookup(symbol: []const u8, do_warn: bool) MalError!*MalType {
|
|
|
|
var optional_mal = repl_environment.getValue(symbol);
|
|
|
|
if(optional_mal) |mal| {
|
|
|
|
return mal.copy(Allocator);
|
|
|
|
}
|
|
|
|
if(do_warn) {
|
|
|
|
const s1 = string_concat(Allocator, "'", symbol) catch return MalError.SystemError;
|
|
|
|
const s2 = string_concat(Allocator, s1, "' not found") catch return MalError.SystemError;
|
|
|
|
defer Allocator.free(s1);
|
|
|
|
defer Allocator.free(s2);
|
|
|
|
warn("'{}' not found.\n", symbol);
|
|
|
|
}
|
|
|
|
return MalError.KeyError;
|
2019-12-08 23:07:41 +03:00
|
|
|
}
|
|
|
|
|
2019-12-19 08:07:11 +03:00
|
|
|
fn eval_ast(mal: *MalType) MalError!*MalType {
|
2019-12-08 23:07:41 +03:00
|
|
|
switch(mal.data) {
|
|
|
|
.Generic => |symbol| {
|
|
|
|
defer mal.delete(Allocator);
|
2019-12-19 08:07:11 +03:00
|
|
|
return lookup(symbol, true);
|
2019-12-08 23:07:41 +03:00
|
|
|
},
|
|
|
|
.List => |*ll| {
|
|
|
|
var new_ll = MalLinkedList.init(Allocator);
|
|
|
|
var iterator = ll.iterator();
|
|
|
|
while(iterator.next()) |next_mal| {
|
2019-12-19 08:07:11 +03:00
|
|
|
const new_mal = try EVAL(next_mal);
|
2019-12-08 23:07:41 +03:00
|
|
|
try linked_list.append_mal(Allocator, &new_ll, new_mal);
|
|
|
|
}
|
|
|
|
linked_list.destroy(Allocator, ll, true);
|
|
|
|
mal.shallow_destroy(Allocator);
|
|
|
|
const ret_mal = MalType.new_list(Allocator, new_ll);
|
|
|
|
return ret_mal;
|
|
|
|
},
|
|
|
|
.Vector => |*ll| {
|
|
|
|
var new_ll = MalLinkedList.init(Allocator);
|
|
|
|
var iterator = ll.iterator();
|
|
|
|
while(iterator.next()) |next_mal| {
|
2019-12-19 08:07:11 +03:00
|
|
|
const new_mal = try EVAL(next_mal);
|
2019-12-08 23:07:41 +03:00
|
|
|
try linked_list.append_mal(Allocator, &new_ll, new_mal);
|
|
|
|
}
|
|
|
|
linked_list.destroy(Allocator, ll, true);
|
|
|
|
mal.shallow_destroy(Allocator);
|
|
|
|
const ret_mal = MalType.new_vector(Allocator, new_ll);
|
|
|
|
return ret_mal;
|
|
|
|
},
|
|
|
|
.HashMap => |hmap| {
|
|
|
|
var new_hashmap = try MalType.new_hashmap(Allocator);
|
|
|
|
var iterator = hmap.iterator();
|
|
|
|
var optional_pair = iterator.next();
|
|
|
|
while(true) {
|
|
|
|
const pair = optional_pair orelse break;
|
|
|
|
const key = pair.key;
|
|
|
|
const value = pair.value;
|
2019-12-19 08:07:11 +03:00
|
|
|
const evaled_value = try EVAL(value);
|
2019-12-08 23:07:41 +03:00
|
|
|
try new_hashmap.hashmap_insert(key, evaled_value);
|
|
|
|
optional_pair = iterator.next();
|
|
|
|
}
|
|
|
|
hash_map.destroy(Allocator, hmap, true);
|
|
|
|
mal.shallow_destroy(Allocator);
|
|
|
|
return new_hashmap;
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return mal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const safeAdd = @import("std").math.add;
|
|
|
|
const safeSub = @import("std").math.sub;
|
|
|
|
const safeMul = @import("std").math.mul;
|
|
|
|
const safeDivFloor = @import("std").math.divFloor;
|
|
|
|
|
|
|
|
fn int_plus(a1: *MalType, a2: *MalType) MalError!*MalType {
|
|
|
|
const x = try a1.as_int();
|
|
|
|
const y = try a2.as_int();
|
|
|
|
const res = safeAdd(i64, x, y) catch return MalError.Overflow;
|
|
|
|
return MalType.new_int(Allocator, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn int_minus(a1: *MalType, a2: *MalType) MalError!*MalType {
|
|
|
|
const x = try a1.as_int();
|
|
|
|
const y = try a2.as_int();
|
|
|
|
const res = safeSub(i64, x, y) catch return MalError.Overflow;
|
|
|
|
return MalType.new_int(Allocator, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn int_mult(a1: *MalType, a2: *MalType) MalError!*MalType {
|
|
|
|
const x = try a1.as_int();
|
|
|
|
const y = try a2.as_int();
|
|
|
|
const res = safeMul(i64, x, y) catch return MalError.Overflow;
|
|
|
|
return MalType.new_int(Allocator, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn int_div(a1: *MalType, a2: *MalType) MalError!*MalType {
|
|
|
|
const x = try a1.as_int();
|
|
|
|
const y = try a2.as_int();
|
|
|
|
const res = safeDivFloor(i64, x, y) catch |err| switch(err) {
|
|
|
|
error.DivisionByZero => return MalError.DivisionByZero,
|
|
|
|
else => return MalError.Overflow,
|
|
|
|
};
|
|
|
|
return MalType.new_int(Allocator, res);
|
|
|
|
}
|
|
|
|
|
2019-12-19 08:07:11 +03:00
|
|
|
fn make_environment() MalError!void {
|
|
|
|
repl_environment = Allocator.create(MalHashMap) catch return MalError.SystemError;
|
|
|
|
repl_environment.* = MalHashMap.init(Allocator);
|
2019-12-08 23:07:41 +03:00
|
|
|
|
|
|
|
const plus_mal = try MalType.new_nil(Allocator);
|
|
|
|
plus_mal.data = MalData{.Fn2 = &int_plus};
|
2019-12-19 08:07:11 +03:00
|
|
|
_ = repl_environment.put("+", plus_mal) catch return MalError.SystemError;
|
2019-12-08 23:07:41 +03:00
|
|
|
const minus_mal = try MalType.new_nil(Allocator);
|
|
|
|
minus_mal.data = MalData{.Fn2 = &int_minus};
|
2019-12-19 08:07:11 +03:00
|
|
|
_ = repl_environment.put("-", minus_mal) catch return MalError.SystemError;
|
2019-12-08 23:07:41 +03:00
|
|
|
const mult_mal = try MalType.new_nil(Allocator);
|
|
|
|
mult_mal.data = MalData{.Fn2 = &int_mult};
|
2019-12-19 08:07:11 +03:00
|
|
|
_ = repl_environment.put("*", mult_mal) catch return MalError.SystemError;
|
2019-12-08 23:07:41 +03:00
|
|
|
const div_mal = try MalType.new_nil(Allocator);
|
|
|
|
div_mal.data = MalData{.Fn2 = &int_div};
|
2019-12-19 08:07:11 +03:00
|
|
|
_ = repl_environment.put("/", div_mal) catch return MalError.SystemError;
|
2019-12-08 23:07:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() !void {
|
|
|
|
const stdout_file = try std.io.getStdOut();
|
2019-12-19 08:07:11 +03:00
|
|
|
try make_environment();
|
2019-12-08 23:07:41 +03:00
|
|
|
while(true) {
|
|
|
|
var line = (try getline(Allocator)) orelse break;
|
2019-12-19 08:07:11 +03:00
|
|
|
var optional_output = rep(line) catch |err| {
|
2019-12-08 23:07:41 +03:00
|
|
|
if(err == MalError.KeyError) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
};
|
2019-12-15 00:39:03 +03:00
|
|
|
if(optional_output) |output| {
|
|
|
|
try stdout_file.write(output);
|
|
|
|
Allocator.free(output);
|
|
|
|
try stdout_file.write("\n");
|
|
|
|
}
|
2019-12-08 23:07:41 +03:00
|
|
|
}
|
|
|
|
}
|