#include "aplwc_internal.h" #include "boot.h" enum aplwc_rc aplwc_new(struct aplwc **p, uint8_t *data, size_t size) { struct aplwc tmp_aplwc; memory_init(&tmp_aplwc, data, size); *p = memory_alloc_aplwc(&tmp_aplwc); if(*p == NULL) return tmp_aplwc.last_error; memcpy(*p, &tmp_aplwc, sizeof(tmp_aplwc)); return aplwc_load_module(*p, "Boot", (const char *)boot_src, boot_src_len); } void aplwc_destroy(struct aplwc *p) { UNUSED_PARAMETER(p); } const char * aplwc_errstr_rc(enum aplwc_rc rc) { switch(rc){ case APLWC_OK: return ""; case APLWC_OUT_OF_MEMORY: return "Out of memory"; case APLWC_NOT_YET_IMPLEMENTED: return "Not yet implemented"; case APLWC_INVALID_UTF8: return "Invalid UTF-8"; case APLWC_INVALID_SYNTAX: return "Invalid syntax"; default: return "(unable to produce error string)"; } } const char * aplwc_errstr(struct aplwc *aplwc) { enum { BUF_SIZE = 256 }; const char *err; enum aplwc_rc rc; err = NULL; rc = aplwc ? aplwc->last_error : APLWC_RC_LAST+1; err = aplwc_errstr_rc(rc); return err; } enum aplwc_rc aplwc_last_error(struct aplwc *aplwc) { return aplwc->last_error; } struct aplwc_line * aplwc_read(struct aplwc *aplwc, const char *line) { struct string *text; struct token_list *tokens; struct aplwc_line *ret; text = chars_to_string(aplwc, line, -1); if(text == NULL) return NULL; tokens = scan_string(aplwc, text, 1); if(tokens == NULL) return NULL; aplwc->last_error = check_tokens(tokens); if(aplwc->last_error != APLWC_OK) return NULL; ret = memory_alloc_aplwc_line(aplwc); if(ret == NULL) return NULL; ret->aplwc = aplwc; ret->text = text; ret->tokens = tokens; return ret; } void aplwc_free_line(struct aplwc_line *line) { if(line) memory_free_root(line->aplwc, line); } struct aplwc_value * aplwc_eval(struct aplwc_line *line) { line->aplwc->last_error = APLWC_OK; return eval_line(line); } void aplwc_free_value(struct aplwc_value *value) { if(value) memory_free_root(value->aplwc, value); } const char * aplwc_value_string(struct aplwc_value *value) { assert(value != NULL); switch(value->tag){ default: return "???"; } } enum aplwc_rc aplwc_load_module(struct aplwc *aplwc, const char *name, const char *src, int srclen) { struct string *text, *namestr; struct token_list *tokens; if((namestr = chars_to_string(aplwc, name, -1)) == NULL) goto error; if((text = chars_to_string(aplwc, src, srclen)) == NULL) goto error; if((tokens = scan_string(aplwc, text, 0)) == NULL) goto error; if(load_module(aplwc, namestr, tokens) != APLWC_OK){ error: return aplwc_last_error(aplwc); } return APLWC_OK; }