2021-11-30 21:29:18 -06:00
|
|
|
#include "script.h"
|
|
|
|
|
2022-08-01 13:32:58 -05:00
|
|
|
#include "stdio.h"
|
2022-06-21 12:48:19 -05:00
|
|
|
|
2022-08-01 13:32:58 -05:00
|
|
|
#include "mruby.h"
|
|
|
|
#include "mruby/compile.h"
|
2022-06-21 12:48:19 -05:00
|
|
|
|
2022-08-01 13:32:58 -05:00
|
|
|
static mrb_state *mrb;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-08-01 13:32:58 -05:00
|
|
|
int fib(int n) {
|
|
|
|
if (n < 2) return n;
|
|
|
|
return fib(n-1) + fib(n-2);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-08-01 13:32:58 -05:00
|
|
|
mrb_value mrb_fib(mrb_state *mrb, mrb_value self) {
|
|
|
|
int n;
|
|
|
|
mrb_get_args(mrb, "i", &n);
|
|
|
|
return mrb_fixnum_value(fib(n));
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-08-01 13:32:58 -05:00
|
|
|
void script_init() {
|
|
|
|
mrb = mrb_open();
|
|
|
|
mrb_define_method(mrb, mrb->object_class, "fib", mrb_fib, MRB_ARGS_REQ(1));
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-08-01 13:32:58 -05:00
|
|
|
void script_run(const char *script) {
|
|
|
|
mrb_load_string(mrb, script);
|
2022-02-06 10:14:57 -06:00
|
|
|
}
|
2022-08-01 13:32:58 -05:00
|
|
|
|
|
|
|
void script_dofile(const char *file) {
|
|
|
|
FILE *mrbf = fopen(file, "r");
|
|
|
|
mrb_load_file(mrb, mrbf);
|
|
|
|
fclose(mrbf);
|
|
|
|
}
|