void kitten_isq(Boxed stack, Boxed definitions) { assert(stack); assert(is_quotation(stack)); Boxed a = pop(stack); push(stack, integer_new(is_quotation(a))); boxed_free(a); }
/* Determine the lexicographic ordering of two quotations. */ int quotation_compare(Boxed a, Boxed b) { if (!a && b) { assert(is_quotation(b)); return -1; } if (a == b) return 0; assert(is_quotation(a)); assert(is_quotation(b)); int a_size = quotation_size(a); int b_size = quotation_size(b); Boxed *a_data = quotation_data(a); Boxed *b_data = quotation_data(b); int minimum = a_size < b_size ? a_size : b_size; int i; for (i = 0; i < minimum; ++i) { int test = boxed_compare(a_data[i], b_data[i]); if (test < 0) return -1; if (test > 0) return +1; } return a_size == b_size ? 0 : a_size < b_size ? -1 : +1; }
/* Copy all values from one quotation to the end of another. */ void quotation_append(Boxed target, Boxed source) { trace("quotation_append()\n"); assert(target); assert(source); assert(is_quotation(target)); assert(is_quotation(source)); const int size = quotation_size(source); int i; for (i = 0; i < size; ++i) quotation_push(target, boxed_copy(quotation_data(source)[i])); }
/* Apply one quotation to another. */ void quotation_apply(Boxed target, Boxed source, Boxed definitions) { assert(target); assert(source); assert(is_quotation(target)); assert(is_quotation(source)); int i; for (i = 0; i < quotation_size(source); ++i) { if (is_word(quotation_data(source)[i])) word_apply(word_value(quotation_data(source)[i]), target, definitions); else quotation_push(target, boxed_copy(quotation_data(source)[i])); } }
void kitten_dup(Boxed stack, Boxed definitions) { trace("kitten_dup()\n"); assert(stack); assert(is_quotation(stack)); Boxed a = boxed_copy(top(stack)); quotation_push(stack, a); }
void kitten_apply(Boxed stack, Boxed definitions) { assert(stack); assert(is_quotation(stack)); Boxed a = pop(stack); quotation_apply(stack, a, definitions); boxed_free(a); }
static int literal_terminator_char(int inputChar) { return inputChar==EOF || character_is_break_out_token(inputChar) || is_whitespace(inputChar) || is_paren(inputChar) || is_quotation(inputChar); }
void kitten_trace(Boxed stack, Boxed definitions) { assert(stack); assert(is_quotation(stack)); printf("[ "); int i; for (i = 0; i < quotation_size(stack); ++i) { Boxed current = quotation_data(stack)[i]; switch (boxed_type(current)) { case FLOAT: printf("%p:%fF ", current, float_value(current)); break; case INTEGER: printf("%p:%ldI ", current, integer_value(current)); break; case WORD: printf("%p:%dW ", current, word_value(current)); break; case QUOTATION: printf("%p:", current); kitten_trace(current, definitions); break; } } printf("] "); }
/* Remove and retrieve the last reference in a quotation. */ Boxed quotation_pop(Boxed quotation_reference) { assert(quotation_reference); assert(is_quotation(quotation_reference)); Quotation *quotation = "ation_reference->value->data.as_quotation; assert(quotation->size > 0); return quotation->data[--quotation->size]; }
//------------------------------------------------------------------ bool code_colorer::is_string_literal(const char_type* p, unsigned* len) const { char_type q = is_quotation(*p); if(q) { char_type mask = 0; if(m_string_mask.size() && m_string_mask[0].length()) { mask = m_string_mask[0][0]; } const char_type* start = p; ++p; while(*p) { if(*p == mask) { ++p; } else { if(*p == q) { ++p; break; } } ++p; } *len = unsigned(p - start); return true; } return false; }
void kitten_eq(Boxed stack, Boxed definitions) { assert(stack); assert(is_quotation(stack)); Boxed b = pop(stack); Boxed a = pop(stack); push(stack, integer_new(boxed_compare(a, b) == 0)); }
void kitten_compose(Boxed stack, Boxed definitions) { trace("kitten_compose()\n"); assert(stack); assert(is_quotation(stack)); Boxed a = pop(stack); if (top(stack)->count == 1) { quotation_append(top(stack), a); } else { Boxed b = pop(stack); assert(is_quotation(b)); Boxed c = boxed_clone(b); boxed_free(b); quotation_append(c, a); push(stack, c); } boxed_free(a); }
void kitten_swap(Boxed stack, Boxed definitions) { assert(stack); assert(is_quotation(stack)); Boxed b = pop(stack); Boxed a = pop(stack); push(stack, b); push(stack, a); }
/* Retrieve the last reference from a quotation. NOTE: Does not automatically call boxed_copy(). */ Boxed quotation_top(Boxed quotation_reference) { assert(quotation_reference); assert(is_quotation(quotation_reference)); Quotation *quotation = "ation_reference->value->data.as_quotation; if (!quotation->size) return NULL; return quotation->data[quotation->size - 1]; }
void word_apply(Word word, Boxed stack, Boxed definitions) { assert(stack); assert(is_quotation(stack)); if (word < 0) { assert(-word - 1 < quotation_size(definitions)); quotation_apply (stack, quotation_data(definitions)[-word - 1], definitions); } else { map[word](stack, definitions); } }
/* Empty the contents of a quotation. */ void quotation_clear(Boxed quotation) { assert(quotation); assert(is_quotation(quotation)); int i; for (i = 0; i < quotation_size(quotation); ++i) boxed_free(quotation_data(quotation)[i]); free(quotation_data(quotation)); quotation->value->data.as_quotation.size = 0; quotation->value->data.as_quotation.capacity = 0; quotation->value->data.as_quotation.data = NULL; }
void kitten_putc(Boxed stack, Boxed definitions) { trace("kitten_putc()\n"); assert(stack); assert(is_quotation(stack)); assert(is_integer(top(stack))); Boxed a = pop(stack); Integer character = integer_unbox(a); assert(character >= 0 && character <= UINT32_MAX); char buffer[5] = { 0 }; utf8_append(character, (uint8_t*)buffer); printf("%s", buffer); }
void kitten_write(Boxed stack, Boxed definitions) { assert(stack); assert(is_quotation(stack)); if (is_integer(top(stack))) { Boxed a = pop(stack); Integer value = integer_unbox(a); printf("%ld", value); } else if (is_float(top(stack))) { Boxed a = pop(stack); Float value = float_unbox(a); printf("%f", value); } }
static thing_th *read_expressions(FILE *src, text_buffer *tb) { int inputChar; tb_clear(tb); while(is_whitespace(inputChar=get_character(src))); if(inputChar==EOF || is_closer(inputChar)) return NULL; if(character_is_break_out_token(inputChar)) return read_subcons(breakout_character(inputChar), src, tb); if(is_quotation(inputChar)) return read_string(inputChar, src, tb); if(is_opener(inputChar)) return read_subcons(open_sub_cons(src, tb, inputChar), src, tb); return read_literals(src, tb, inputChar); }
void kitten_if(Boxed stack, Boxed definitions) { assert(stack); assert(is_quotation(stack)); Boxed else_branch = pop(stack); Boxed then_branch = pop(stack); Boxed condition = pop(stack); assert(is_integer(condition)); if (integer_unbox(condition)) { boxed_free(else_branch); push(stack, then_branch); kitten_apply(stack, definitions); } else { boxed_free(then_branch); push(stack, else_branch); kitten_apply(stack, definitions); } }
/* Add a reference to the end of a quotation. NOTE: Takes ownership of the reference, i.e., does not automatically call boxed_copy(). */ void quotation_push(Boxed quotation_reference, Boxed reference) { trace("quotation_push()\n"); assert(quotation_reference); assert(is_quotation(quotation_reference)); Quotation *quotation = "ation_reference->value->data.as_quotation; int capacity = quotation->capacity == 0 ? 1 : quotation->size == quotation->capacity ? quotation->capacity * 2 : 0; if (capacity) { Boxed *data = realloc(quotation->data, capacity * sizeof(Boxed)); if (!data) return; quotation->capacity = capacity; quotation->data = data; } quotation->data[quotation->size++] = reference; }
void kitten_pop(Boxed stack, Boxed definitions) { assert(stack); assert(is_quotation(stack)); boxed_free(quotation_pop(stack)); }
/* Retrieve the size of a quotation. */ int quotation_size(Boxed quotation) { assert(quotation); assert(is_quotation(quotation)); return quotation->value->data.as_quotation.size; }
void kitten_quote(Boxed stack, Boxed definitions) { assert(stack); assert(is_quotation(stack)); Boxed a = pop(stack); push(stack, quotation_new(1, a)); }
/* Retrieve the data from a quotation. */ Boxed *quotation_data(Boxed quotation) { assert(quotation); assert(is_quotation(quotation)); return quotation->value->data.as_quotation.data; }
void push(Boxed stack, Boxed reference) { assert(stack); assert(is_quotation(stack)); quotation_push(stack, reference); }
Boxed top(Boxed stack) { assert(stack); assert(is_quotation(stack)); return quotation_top(stack); }