Example #1
0
File: v7.c Project: di3online/v7
static enum v7_err parse_if_statement(struct v7 *v7) {
  int old_no_exec = v7->no_exec;

  TRY(match(v7, '('));
  TRY(parse_expression(v7));
  TRY(match(v7, ')'));
  assert(v7->no_exec || v7->sp > 0);  // Stack may be empty if v7->no_exec
  if (!v7->no_exec && !v7_is_true(v7_top(v7)[-1])) {
    v7->no_exec = 1;
  }
  TRY(parse_compound_statement(v7));
  v7->no_exec = old_no_exec;

  return V7_OK;
}
Example #2
0
ICACHE_FLASH_ATTR static v7_val_t GPIO_out(struct v7 *v7, v7_val_t this_obj,
                                           v7_val_t args) {
  v7_val_t pinv = v7_array_get(v7, args, 0);
  v7_val_t valv = v7_array_get(v7, args, 1);
  int pin, val;

  if (!v7_is_double(pinv)) {
    printf("non-numeric pin\n");
    return v7_create_undefined();
  }
  pin = v7_to_double(pinv);
  val = v7_is_true(v7, valv) ? 1 : 0;

  set_gpio(pin, val);

  return v7_create_undefined();
}
Example #3
0
File: v7.c Project: di3online/v7
//  expression  =   term { add_op term } |
//                  expression "?" expression ":" expression
//                  expression logical_op expression
//                  variable "=" expression
//  add_op      =   "+" | "-"
static enum v7_err parse_expression(struct v7 *v7) {
#ifdef V7_DEBUG
  const char *stmt_str = v7->cursor;
#endif
  int op;

  v7->cur_obj = &v7->scopes[v7->current_scope];
  TRY(parse_term(v7));

  while (*v7->cursor == '-' || *v7->cursor == '+') {
    int ch = *v7->cursor;
    TRY(match(v7, ch));
    TRY(parse_term(v7));
    TRY(do_arithmetic_op(v7, ch));
  }

  if ((op = is_logical_op(v7->cursor)) > OP_XX) {
    v7->cursor += op == OP_LT || op == OP_GT ? 1 : 2;
    skip_whitespaces_and_comments(v7);
    TRY(parse_expression(v7));
    TRY(do_logical_op(v7, op));
  }

  // Parse assignment
  if (*v7->cursor == '=') {
    //printf("=> cur_obj: %p\n", cur_obj);
    TRY(parse_assignment(v7, v7->cur_obj));
  }

  // Parse ternary operator
  if (*v7->cursor == '?') {
    int condition_true = v7_is_true(v7_top(v7)[-1]);
    int old_no_exec = v7->no_exec;

    TRY(match(v7, '?'));
    v7->no_exec = old_no_exec || !condition_true;
    TRY(parse_expression(v7));
    TRY(match(v7, ':'));
    v7->no_exec = old_no_exec || condition_true;
    TRY(parse_expression(v7));
    v7->no_exec = old_no_exec;
  }

  return V7_OK;
}
Example #4
0
static enum v7_err GPIO_write(struct v7 *v7, v7_val_t *res) {
  v7_val_t pinv = v7_arg(v7, 0);
  v7_val_t valv = v7_arg(v7, 1);
  int pin, val;

  if (!v7_is_number(pinv)) {
    printf("non-numeric pin\n");
    *res = v7_create_undefined();
  } else {
    pin = v7_to_number(pinv);

    /*
     * We assume 0 if the value is "falsy",
     * and 1 if the value is "truthy"
     */
    val = !!v7_is_true(v7, valv);

    *res = v7_create_boolean(sj_gpio_write(pin, val) == 0);
  }

  return V7_OK;
}
Example #5
0
static int test_if_expr(struct v7 *v7, const char *expr, int result) {
  struct v7_val *v = v7_exec(v7, expr);
  return v != NULL && (v7_is_true(v) ? 1 : 0) == result;
}
Example #6
0
static int test_if_expr(struct v7 *v7, const char *expr, int result) {
  return v7_exec(v7, expr) == V7_OK && v7_sp(v7) == 1 &&
    (v7_is_true(v7_top(v7)[-1]) ? 1 : 0) == result;
}