ilovapples/mml
A minimal mathematical scripting language, written in Zig
9c477eb28415cf46e1c52f6b1143d4d0da8d5d36Original repository with C implementation: ilovapples/maths
MML (My Math Language or Minimal Math Language, or something else) is a WIP mathematical scripting language. That is, it's a
calculator with more functionality in some areas, less in others (depending on the calculator).
A copy of the original README file is at the bottom, but a good bit may be outdated.
The documentation is here.
The program (library + application) has been tested (and are functional) on M1 MacOS and on x86_64 Linux. Neither have been tested on Windows, but there aren't any outstanding reasons they wouldn't work. Any tests, contributed in the Issues page, would be greatly appreciated.
The project is built to be used as a library (the executable is actually just a sample usage of that library). Here's an example of a simple usage of the library:
const std = @import("std");
const mml = @import("mml");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}).init;
defer _ = gpa.deinit();
var arena = std.heap.ArenaAllocator.init(gpa.allocator());
defer arena.deinit();
var write_buffer: [512]u8 = undefined;
var file_writer = std.fs.File.stdout().writer(&write_buffer);
const stdout = &file_writer.interface;
var eval: mml.Evaluator = try .init(&arena, null);
defer eval.deinit();
var config: mml.Config = .{ .writer = stdout };
eval.conf = &config;
config.evaluator = &eval;
const parsed = try mml.parse.parseExpr(&arena, "5 + 9cos{2.3}");
const val = try eval.eval(parsed);
try val.printValue(config);
try stdout.flush();
}
(this code is replicated in tests/example-use-1.zig)
You could compile this like any other library with build.zig.
it's a thing I guess Also, this program has been tested on x86_64-linux and aarch64-macos. The prompt (REPL) definitely won't work on Windows, and the rest of the program hasn't been tested (and I can't be bothered to check for anything POSIX-specific).
Not much, but there is a syntax guide here if you want to read it.
git submodule update --init # first time
make
Then run build/mml --help to display the command-line options.
If you have zig installed, you can use zig build to build the library and executable. Here are the commands:
git submodule update --init # first time
zig build
You can run zig build -Doptimize=ReleaseFast to get more optimization. zig build also allows
easy cross-compilation; this page has some more information (should still be up to date-ish).
This will automatically build the static library as well (every source file except src/main.c and src/prompt.c).
It's not much of a library, but it is built to be easily extendable (hopefully that's true). A minimal example:
#include <stdint.h>
#include "mml/mml.h"
int32_t main(void)
{
MML_state *state = MML_init_state();
const char *s = "cos{1.5pi} == 0.0";
MML_Value ret = MML_eval_parse(state, s);
MML_println_typedval(state, &ret);
MML_cleanup_state(state);
return 0;
}
And it can be compiled with this command (assuming you've run make shared_lib or make static_lib, are currently in the root directory, and named the example file test.c):
gcc -o test test.c -Iincl -Lbuild -lmml -lm
The shared object file is a bit finnicky, so if you link with the shared library, you'll probably have to do this (the above should work on OS X, though):
gcc -o test test.c -Iincl build/libmml.so