HelenOS sources
This source file includes following definitions.
- builtin_declare
- builtin_bind
- builtin_get_gf_class
- builtin_new
- builtin_code_snippet
- builtin_find_lvl0
- builtin_find_lvl1
- builtin_fun_bind
- builtin_run_proc
- builtin_get_self_mbr_var
- builtin_return_string
- builtin_declare_fun
- builtin_fun_add_arg
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "builtin/bi_boxed.h"
#include "builtin/bi_error.h"
#include "builtin/bi_char.h"
#include "builtin/bi_console.h"
#include "builtin/bi_int.h"
#include "builtin/bi_task.h"
#include "builtin/bi_textfile.h"
#include "builtin/bi_string.h"
#include "input.h"
#include "intmap.h"
#include "lex.h"
#include "list.h"
#include "mytypes.h"
#include "os/os.h"
#include "parse.h"
#include "rdata.h"
#include "run.h"
#include "stree.h"
#include "strtab.h"
#include "symbol.h"
#include "builtin.h"
static builtin_t *builtin_new(void);
void builtin_declare(stree_program_t *program)
{
builtin_t *bi;
bi = builtin_new();
bi->program = program;
program->builtin = bi;
builtin_code_snippet(bi,
"class Object is\n"
"end\n");
bi->gf_class = builtin_find_lvl0(bi, "Object");
bi_error_declare(bi);
bi_char_declare(bi);
bi_console_declare(bi);
bi_int_declare(bi);
bi_task_declare(bi);
bi_textfile_declare(bi);
bi_string_declare(bi);
}
void builtin_bind(builtin_t *bi)
{
bi_boxed_bind(bi);
bi_error_bind(bi);
bi_char_bind(bi);
bi_console_bind(bi);
bi_int_bind(bi);
bi_task_bind(bi);
bi_textfile_bind(bi);
bi_string_bind(bi);
}
stree_csi_t *builtin_get_gf_class(builtin_t *builtin)
{
if (builtin->gf_class == NULL)
return NULL;
return symbol_to_csi(builtin->gf_class);
}
static builtin_t *builtin_new(void)
{
builtin_t *builtin;
builtin = calloc(1, sizeof(builtin_t));
if (builtin == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
return builtin;
}
void builtin_code_snippet(builtin_t *bi, const char *snippet)
{
input_t *input;
lex_t lex;
parse_t parse;
input_new_string(&input, snippet);
lex_init(&lex, input);
parse_init(&parse, bi->program, &lex);
parse_module(&parse);
}
stree_symbol_t *builtin_find_lvl0(builtin_t *bi, const char *sym_name)
{
stree_symbol_t *sym;
stree_ident_t *ident;
ident = stree_ident_new();
ident->sid = strtab_get_sid(sym_name);
sym = symbol_lookup_in_csi(bi->program, NULL, ident);
assert(sym != NULL);
return sym;
}
stree_symbol_t *builtin_find_lvl1(builtin_t *bi, const char *csi_name,
const char *sym_name)
{
stree_symbol_t *csi_sym;
stree_csi_t *csi;
stree_symbol_t *mbr_sym;
stree_ident_t *ident;
ident = stree_ident_new();
ident->sid = strtab_get_sid(csi_name);
csi_sym = symbol_lookup_in_csi(bi->program, NULL, ident);
assert(csi_sym != NULL);
csi = symbol_to_csi(csi_sym);
assert(csi != NULL);
ident->sid = strtab_get_sid(sym_name);
mbr_sym = symbol_lookup_in_csi(bi->program, csi, ident);
assert(mbr_sym != NULL);
return mbr_sym;
}
void builtin_fun_bind(builtin_t *bi, const char *csi_name,
const char *sym_name, builtin_proc_t bproc)
{
stree_symbol_t *fun_sym;
stree_fun_t *fun;
fun_sym = builtin_find_lvl1(bi, csi_name, sym_name);
assert(fun_sym != NULL);
fun = symbol_to_fun(fun_sym);
assert(fun != NULL);
fun->proc->bi_handler = bproc;
}
void builtin_run_proc(run_t *run, stree_proc_t *proc)
{
stree_symbol_t *fun_sym;
builtin_proc_t bproc;
#ifdef DEBUG_RUN_TRACE
printf("Run builtin procedure.\n");
#endif
fun_sym = proc->outer_symbol;
bproc = proc->bi_handler;
if (bproc == NULL) {
printf("Error: Unrecognized builtin function '");
symbol_print_fqn(fun_sym);
printf("'.\n");
exit(1);
}
(*bproc)(run);
}
rdata_var_t *builtin_get_self_mbr_var(run_t *run, const char *mbr_name)
{
run_proc_ar_t *proc_ar;
rdata_object_t *object;
sid_t mbr_name_sid;
rdata_var_t *mbr_var;
proc_ar = run_get_current_proc_ar(run);
assert(proc_ar->obj->vc == vc_object);
object = proc_ar->obj->u.object_v;
mbr_name_sid = strtab_get_sid(mbr_name);
mbr_var = intmap_get(&object->fields, mbr_name_sid);
assert(mbr_var != NULL);
return mbr_var;
}
void builtin_return_string(run_t *run, const char *astr)
{
rdata_string_t *rstring;
rdata_var_t *rvar;
rdata_value_t *rval;
rdata_item_t *ritem;
run_proc_ar_t *proc_ar;
#ifdef DEBUG_RUN_TRACE
printf("Return string '%s' from builtin function.\n", astr);
#endif
rstring = rdata_string_new();
rstring->value = astr;
rvar = rdata_var_new(vc_string);
rvar->u.string_v = rstring;
rval = rdata_value_new();
rval->var = rvar;
ritem = rdata_item_new(ic_value);
ritem->u.value = rval;
proc_ar = run_get_current_proc_ar(run);
proc_ar->retval = ritem;
}
stree_symbol_t *builtin_declare_fun(stree_csi_t *csi, const char *name)
{
stree_ident_t *ident;
stree_fun_t *fun;
stree_fun_sig_t *sig;
stree_csimbr_t *csimbr;
stree_symbol_t *fun_sym;
stree_symbol_attr_t *sym_attr;
ident = stree_ident_new();
ident->sid = strtab_get_sid(name);
fun = stree_fun_new();
fun->name = ident;
fun->proc = stree_proc_new();
fun->proc->body = NULL;
sig = stree_fun_sig_new();
fun->sig = sig;
list_init(&fun->sig->args);
csimbr = stree_csimbr_new(csimbr_fun);
csimbr->u.fun = fun;
fun_sym = stree_symbol_new(sc_fun);
fun_sym->u.fun = fun;
fun_sym->outer_csi = csi;
sym_attr = stree_symbol_attr_new(sac_static);
list_append(&fun_sym->attr, sym_attr);
fun->symbol = fun_sym;
fun->proc->outer_symbol = fun_sym;
list_append(&csi->members, csimbr);
return fun_sym;
}
void builtin_fun_add_arg(stree_symbol_t *fun_sym, const char *name)
{
stree_proc_arg_t *proc_arg;
stree_fun_t *fun;
fun = symbol_to_fun(fun_sym);
assert(fun != NULL);
proc_arg = stree_proc_arg_new();
proc_arg->name = stree_ident_new();
proc_arg->name->sid = strtab_get_sid(name);
proc_arg->type = NULL;
list_append(&fun->sig->args, proc_arg);
}
HelenOS homepage, sources at GitHub