27a56b88a287801e5996f94b8f9b2820ba6ceedb — Simon Ser a month ago 7a7ea0d
arithm: introduce mrsh_arithm_variable

References: https://github.com/emersion/mrsh/issues/85
3 files changed, 38 insertions(+), 2 deletions(-)

M arithm.c
M include/mrsh/arithm.h
M shell/arithm.c
M arithm.c => arithm.c +22 -0
@@ 12,6 12,11 @@ struct mrsh_arithm_literal *al = mrsh_arithm_expr_get_literal(expr);
  		free(al);
  		return;
+ 	case MRSH_ARITHM_VARIABLE:;
+ 		struct mrsh_arithm_variable *av = mrsh_arithm_expr_get_variable(expr);
+ 		free(av->name);
+ 		free(av);
+ 		return;
  	case MRSH_ARITHM_UNOP:;
  		struct mrsh_arithm_unop *au = mrsh_arithm_expr_get_unop(expr);
  		mrsh_arithm_expr_destroy(au->body);


@@ 51,6 56,17 @@ return al;
  }
  
+ struct mrsh_arithm_variable *mrsh_arithm_variable_create(char *name) {
+ 	struct mrsh_arithm_variable *av =
+ 		calloc(1, sizeof(struct mrsh_arithm_variable));
+ 	if (av == NULL) {
+ 		return NULL;
+ 	}
+ 	av->expr.type = MRSH_ARITHM_VARIABLE;
+ 	av->name = name;
+ 	return av;
+ }
+ 
  struct mrsh_arithm_unop *mrsh_arithm_unop_create(
  		enum mrsh_arithm_unop_type type, struct mrsh_arithm_expr *body) {
  	struct mrsh_arithm_unop *au =


@@ 115,6 131,12 @@ return (struct mrsh_arithm_literal *)expr;
  }
  
+ struct mrsh_arithm_variable *mrsh_arithm_expr_get_variable(
+ 		const struct mrsh_arithm_expr *expr) {
+ 	assert(expr->type == MRSH_ARITHM_VARIABLE);
+ 	return (struct mrsh_arithm_variable *)expr;
+ }
+ 
  struct mrsh_arithm_unop *mrsh_arithm_expr_get_unop(
  		const struct mrsh_arithm_expr *expr) {
  	assert(expr->type == MRSH_ARITHM_UNOP);

M include/mrsh/arithm.h => include/mrsh/arithm.h +10 -0
@@ 3,6 3,7 @@   enum mrsh_arithm_expr_type {
  	MRSH_ARITHM_LITERAL,
+ 	MRSH_ARITHM_VARIABLE,
  	MRSH_ARITHM_UNOP,
  	MRSH_ARITHM_BINOP,
  	MRSH_ARITHM_COND,


@@ 12,6 13,7 @@ /**
   * An aritmetic expression. One of:
   * - A literal
+  * - A variable
   * - An unary operation
   * - A binary operation
   * - A condition


@@ 26,6 28,11 @@ long value;
  };
  
+ struct mrsh_arithm_variable {
+ 	struct mrsh_arithm_expr expr;
+ 	char *name;
+ };
+ 
  enum mrsh_arithm_unop_type {
  	MRSH_ARITHM_UNOP_PLUS,
  	MRSH_ARITHM_UNOP_MINUS,


@@ 94,6 101,7 @@   void mrsh_arithm_expr_destroy(struct mrsh_arithm_expr *expr);
  struct mrsh_arithm_literal *mrsh_arithm_literal_create(long value);
+ struct mrsh_arithm_variable *mrsh_arithm_variable_create(char *name);
  struct mrsh_arithm_unop *mrsh_arithm_unop_create(
  	enum mrsh_arithm_unop_type type, struct mrsh_arithm_expr *body);
  struct mrsh_arithm_binop *mrsh_arithm_binop_create(


@@ 107,6 115,8 @@ struct mrsh_arithm_expr *value);
  struct mrsh_arithm_literal *mrsh_arithm_expr_get_literal(
  	const struct mrsh_arithm_expr *expr);
+ struct mrsh_arithm_variable *mrsh_arithm_expr_get_variable(
+ 	const struct mrsh_arithm_expr *expr);
  struct mrsh_arithm_unop *mrsh_arithm_expr_get_unop(
  	const struct mrsh_arithm_expr *expr);
  struct mrsh_arithm_binop *mrsh_arithm_expr_get_binop(

M shell/arithm.c => shell/arithm.c +6 -2
@@ 183,7 183,11 @@ struct mrsh_arithm_literal *literal =
  			(struct mrsh_arithm_literal *)expr;
  		*result = literal->value;
- 		break;
+ 		return true;
+ 	case MRSH_ARITHM_VARIABLE:;
+ 		//struct mrsh_arithm_variable *variable =
+ 		//	(struct mrsh_arithm_variable *)expr;
+ 		assert(false); // TODO
  	case MRSH_ARITHM_BINOP:;
  		struct mrsh_arithm_binop *binop =
  			(struct mrsh_arithm_binop *)expr;


@@ 201,5 205,5 @@ (struct mrsh_arithm_assign *)expr;
  		return run_arithm_assign(state, assign, result);
  	}
- 	return true;
+ 	assert(false);
  }