Ejemplo n.º 1
0
static int parse_avt(elcgen *gen, xmlNodePtr n, const char *attr,
                     expression **expr, int required, expression *pnode)
{
  char *value;
  char *tmp;
  assert(pnode->xmlnode == n);
  if (!xmlHasProp(n,attr)) {
    *expr = NULL;
    if (required)
      return gen_error(gen,"%s element missing %s attribute",n->name,attr);
    else
      return 1;
  }

  value = xmlGetProp(n,attr);
  if (gen->ispattern && (!strncmp(value,"#E",2))) {
    *expr = new_expression(XPATH_DSVAR);
    (*expr)->str = strdup(value+1);
  }
  else {
    tmp = (char*)malloc(strlen(value)+3);
    sprintf(tmp,"}%s{",value);
    *expr = parse_xpath(gen,pnode,tmp);
    free(tmp);
  }
  free(value);

  return (NULL != *expr);
}
Ejemplo n.º 2
0
struct expression *new_var_expression(int var_id)
{
	struct expression *exp = new_expression();
	exp->type = VarExp;
	exp->u.var_id = var_id;
	return exp;
}
Ejemplo n.º 3
0
expression *new_expression2(int type, expression *left, expression *right)
{
  expression *expr = new_expression(type);
  expr->r.left = left;
  expr->r.right = right;
  return expr;
}
Ejemplo n.º 4
0
struct expression *new_op_expression(expression_type type,
				     struct expression * left,
				     struct expression *right)
{
	struct expression *exp = new_expression();
	exp->type = type;
	exp->u.left = left;
	exp->u.right = right;
	return exp;
}
Ejemplo n.º 5
0
/*
 * parse a comma separated expression list like a, b, c
 * return 0 on success, -1 on error
 * parsed expressions are returned in **e
 */
static int parse_expression_list(char *str, expression **e) 
{
	int start=0, i=-1, j=-1, apost=0;
	char str2[EXPRESSION_LENGTH];
	expression *e1=NULL, *e2;
	
	if (!str || !e) return -1;

	*e = NULL;
	do {
		i++;
		switch(str[i]) {
			case '"':	apost = !apost;
					break;
			case ',':	if (apost) break;
			case '\0':	/* word found */
					while ((str[start] == ' ') || (str[start] == '\t')) start++;
					if (str[start] == '"') start++;
					j = i-1;
					while ((0 < j) && ((str[j] == ' ') || (str[j] == '\t'))) j--;
					if ((0 < j) && (str[j] == '"')) j--;
					if (start<=j) {
						/* valid word */
						if (j-start+1 >= EXPRESSION_LENGTH) {
							/* error */
							LOG(L_ERR, "ERROR: parse_expression_list(): " \
								"too long expression, increase EXPRESSION_LENGTH\n");
							goto error;
						}
						strncpy(str2, str+start, j-start+1);
						str2[j-start+1] = '\0';
						
						e2 = new_expression(str2);
						if (!e2) {
							/* memory error */
							LOG(L_ERR, "ERROR: parse_expression_list(): " \
								"not enough memory\n");
							goto error;
						}
						
						if (e1) {
							/* it is not the first */
							e1->next = e2;
							e1 = e2;
						} else {
							/* it is the first */
							*e = e1 = e2;
						}
					} else {
						/* parsing error */
						LOG(L_ERR, "ERROR: parse_expression_list(): " \
							"expression parsing error\n");
						goto error;
					}
					/* for the next word */
					start = i+1;
		}
	} while (str[i] != '\0');
	
	return 0;

error:
	if (*e) {
		free_expression(*e);
		*e = NULL;
	}
	return -1;
}
Ejemplo n.º 6
0
struct expression_s *
parse (FILE *input)
{
  int ch;
  do {
    ch = getc (input);
    if ( ch == '#' )
      while ( ch != '\n' && ch != EOF )
	ch = getc (input);
  } while ( ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' );
  if ( ch == '`' )
    {
      struct expression_s *rator = parse (input);
      struct expression_s *rand = parse (input);
      struct expression_s *expr = new_expression ();

      expr->t = EXPRESSION_APPLICATION;
      init_ptr (&expr->d.expression_application_v.rator, rator);
      init_ptr (&expr->d.expression_application_v.rand, rand);
#if 0  /* Harmless but not necessary */
      free_expression (rator);
      free_expression (rand);
#endif
      return expr;
    }
  else if ( ch == 'i' || ch == 'I' )
    {
      struct function_s *fun = new_function ();
      struct expression_s *expr = new_expression ();

      fun->t = FUNCTION_I;
      expr->t = EXPRESSION_FUNCTION;
      init_ptr (&expr->d.expression_function_v, fun);
#if 0  /* Harmless but not necessary */
      free_function (fun);
#endif
      return expr;
    }
  else if ( ch == 'k' || ch == 'K' )
    {
      struct function_s *fun = new_function ();
      struct expression_s *expr = new_expression ();

      fun->t = FUNCTION_K;
      expr->t = EXPRESSION_FUNCTION;
      init_ptr (&expr->d.expression_function_v, fun);
#if 0  /* Harmless but not necessary */
      free_function (fun);
#endif
      return expr;
    }
  else if ( ch == 's' || ch == 'S' )
    {
      struct function_s *fun = new_function ();
      struct expression_s *expr = new_expression ();

      fun->t = FUNCTION_S;
      expr->t = EXPRESSION_FUNCTION;
      init_ptr (&expr->d.expression_function_v, fun);
#if 0  /* Harmless but not necessary */
      free_function (fun);
#endif
      return expr;
    }
  else if ( ch == 'v' || ch == 'V' )
    {
      struct function_s *fun = new_function ();
      struct expression_s *expr = new_expression ();

      fun->t = FUNCTION_V;
      expr->t = EXPRESSION_FUNCTION;
      init_ptr (&expr->d.expression_function_v, fun);
#if 0  /* Harmless but not necessary */
      free_function (fun);
#endif
      return expr;
    }
  else if ( ch == 'd' || ch == 'D' )
    {
      struct function_s *fun = new_function ();
      struct expression_s *expr = new_expression ();

      fun->t = FUNCTION_D;
      expr->t = EXPRESSION_FUNCTION;
      init_ptr (&expr->d.expression_function_v, fun);
#if 0  /* Harmless but not necessary */
      free_function (fun);
#endif
      return expr;
    }
  else if ( ch == 'e' || ch == 'E' )
    {
      struct function_s *fun = new_function ();
      struct expression_s *expr = new_expression ();

      fun->t = FUNCTION_E;
      expr->t = EXPRESSION_FUNCTION;
      init_ptr (&expr->d.expression_function_v, fun);
#if 0  /* Harmless but not necessary */
      free_function (fun);
#endif
      return expr;
    }
  else if ( ch == 'p' || ch == 'P' )
    {
      struct function_s *fun = new_function ();
      struct expression_s *expr = new_expression ();

      fun->t = FUNCTION_P;
      expr->t = EXPRESSION_FUNCTION;
      init_ptr (&expr->d.expression_function_v, fun);
#if 0  /* Harmless but not necessary */
      free_function (fun);
#endif
      return expr;
    }
  else if ( ch == 'f' || ch == 'F' )
    {
      struct function_s *fun = new_function ();
      struct expression_s *expr = new_expression ();

      fun->t = FUNCTION_F;
      expr->t = EXPRESSION_FUNCTION;
      init_ptr (&expr->d.expression_function_v, fun);
#if 0  /* Harmless but not necessary */
      free_function (fun);
#endif
      return expr;
    }
  else if ( ch == 'r' )
    {
      struct function_s *fun = new_function ();
      struct expression_s *expr = new_expression ();

      fun->t = FUNCTION_DOT;
      fun->d.function_dot_v = '\n';
      expr->t = EXPRESSION_FUNCTION;
      init_ptr (&expr->d.expression_function_v, fun);
#if 0  /* Harmless but not necessary */
      free_function (fun);
#endif
      return expr;
    }
  else if ( ch == '.' )
    {
      struct function_s *fun = new_function ();
      struct expression_s *expr = new_expression ();
      int ch2;

      fun->t = FUNCTION_DOT;
      ch2 = getc (input);
      if ( ch2 == EOF )
	goto ueof;
      fun->d.function_dot_v = ch2;
      expr->t = EXPRESSION_FUNCTION;
      init_ptr (&expr->d.expression_function_v, fun);
#if 0  /* Harmless but not necessary */
      free_function (fun);
#endif
      return expr;
    }
  else if ( ch == '@' )
    {
      struct function_s *fun = new_function ();
      struct expression_s *expr = new_expression ();

      fun->t = FUNCTION_AT;
      expr->t = EXPRESSION_FUNCTION;
      init_ptr (&expr->d.expression_function_v, fun);
#if 0  /* Harmless but not necessary */
      free_function (fun);
#endif
      return expr;
    }
  else if ( ch == '?' )
    {
      struct function_s *fun = new_function ();
      struct expression_s *expr = new_expression ();
      int ch2;

      fun->t = FUNCTION_QUES;
      ch2 = getc (input);
      if ( ch2 == EOF )
	goto ueof;
      fun->d.function_ques_v = ch2;
      expr->t = EXPRESSION_FUNCTION;
      init_ptr (&expr->d.expression_function_v, fun);
#if 0  /* Harmless but not necessary */
      free_function (fun);
#endif
      return expr;
    }
  else if ( ch == '|' )
    {
      struct function_s *fun = new_function ();
      struct expression_s *expr = new_expression ();

      fun->t = FUNCTION_PIPE;
      expr->t = EXPRESSION_FUNCTION;
      init_ptr (&expr->d.expression_function_v, fun);
#if 0  /* Harmless but not necessary */
      free_function (fun);
#endif
      return expr;
    }
  else if ( ch == EOF )
    {
    ueof:
      fprintf (stderr, "Unexpected end of file\n");
      exit (1);
    }
  else
    {
      fprintf (stderr, "Character not recognized: %c\n", ch);
      exit (1);
    }
  return NULL;
}
Ejemplo n.º 7
0
struct task_s *
apply (struct function_s *rator, struct function_s *rand,
       struct continuation_s *cont)
{
  switch ( rator->t )
    {
    case FUNCTION_I:
      return invoke (cont, rand);
    case FUNCTION_DOT:
      putchar (rator->d.function_dot_v);
      return invoke (cont, rand);
    case FUNCTION_K1:
      return invoke (cont, rator->d.function_k1_v);
    case FUNCTION_K:
      {
	struct function_s *val = new_function ();
	struct task_s *task;

	val->t = FUNCTION_K1;
	init_ptr (&val->d.function_k1_v, rand);
	task = invoke (cont, val);
	free_function (val);
	return task;
      }
    case FUNCTION_S2:
      {
	struct expression_s *e_x = new_expression ();
	struct expression_s *e_y = new_expression ();
	struct expression_s *e_z = new_expression ();
	struct expression_s *e1 = new_expression ();
	struct expression_s *e2 = new_expression ();
	struct expression_s *e = new_expression ();
	struct task_s *task = new_task ();

	e_x->t = EXPRESSION_FUNCTION;
	init_ptr (&e_x->d.expression_function_v, rator->d.function_s2_v.x);
	e_y->t = EXPRESSION_FUNCTION;
	init_ptr (&e_y->d.expression_function_v, rator->d.function_s2_v.y);
	e_z->t = EXPRESSION_FUNCTION;
	init_ptr (&e_z->d.expression_function_v, rand);
	e1->t = EXPRESSION_APPLICATION;
	init_ptr (&e1->d.expression_application_v.rator, e_x);
	init_ptr (&e1->d.expression_application_v.rand, e_z);
	e2->t = EXPRESSION_APPLICATION;
	init_ptr (&e2->d.expression_application_v.rator, e_y);
	init_ptr (&e2->d.expression_application_v.rand, e_z);
	e->t = EXPRESSION_APPLICATION;
	init_ptr (&e->d.expression_application_v.rator, e1);
	init_ptr (&e->d.expression_application_v.rand, e2);
	task->t = TASK_EVAL;
	init_ptr (&task->d.task_eval_v.expr, e);
	init_ptr (&task->d.task_eval_v.cont, cont);
#if 0  /* Harmless but not necessary */
	free_expression (e_x);
	free_expression (e_y);
	free_expression (e_z);
	free_expression (e1);
	free_expression (e2);
	free_expression (e);
#endif
	return task;
      }
    case FUNCTION_S1:
      {
	struct function_s *val = new_function ();
	struct task_s *task;

	val->t = FUNCTION_S2;
	init_ptr (&val->d.function_s2_v.x, rator->d.function_s1_v);
	init_ptr (&val->d.function_s2_v.y, rand);
	task = invoke (cont, val);
	free_function (val);
	return task;
      }
    case FUNCTION_S:
      {
	struct function_s *val = new_function ();
	struct task_s *task;

	val->t = FUNCTION_S1;
	init_ptr (&val->d.function_s1_v, rand);
	task = invoke (cont, val);
	free_function (val);
	return task;
      }
    case FUNCTION_V:
      return invoke (cont, rator);
    case FUNCTION_D1:
      {
	struct continuation_s *ncont = new_continuation ();
	struct task_s *task = new_task ();

	ncont->t = CONTINUATION_DEL;
	init_ptr (&ncont->d.continuation_del_v.erand, rand);
	init_ptr (&ncont->d.continuation_del_v.cont, cont);
	task->t = TASK_EVAL;
	init_ptr (&task->d.task_eval_v.expr, rator->d.function_d1_v);
	init_ptr (&task->d.task_eval_v.cont, ncont);
#if 0  /* Harmless but not necessary */
	free_continuation (ncont);
#endif
	return task;
      }
    case FUNCTION_D:
      {
	struct expression_s *promise = new_expression ();
	struct function_s *val = new_function ();
	struct task_s *task;

	promise->t = EXPRESSION_FUNCTION;
	init_ptr (&promise->d.expression_function_v, rand);
	val->t = FUNCTION_D1;
	init_ptr (&val->d.function_d1_v, promise);
	task = invoke (cont, val);
	free_continuation (cont);
	free_function (val);
	return task;
      }
    case FUNCTION_CONT:
      return invoke (rator->d.function_cont_v, rand);
    case FUNCTION_DCONT:
      {
	struct function_s *val = new_function ();
	struct task_s *task = new_task ();
	struct continuation_s *ncont = new_continuation ();

	push_cont (cont);
	val->t = FUNCTION_CONT;
	init_ptr (&val->d.function_cont_v, rator->d.function_cont_v);
	task->t = TASK_APP;
	ncont->t = CONTINUATION_ABORT;
	init_ptr (&task->d.task_app_v.erator, val);
	init_ptr (&task->d.task_app_v.erand, rand);
	init_ptr (&task->d.task_app_v.cont, ncont);
	return task;
      }
    case FUNCTION_P:
      {
	struct function_s *val = new_function ();
	struct task_s *task = new_task ();
	struct continuation_s *ncont = new_continuation ();

	push_cont (cont);
	val->t = FUNCTION_I;
	task->t = TASK_APP;
	ncont->t = CONTINUATION_ABORT;
	init_ptr (&task->d.task_app_v.erator, rand);
	init_ptr (&task->d.task_app_v.erand, val);
	init_ptr (&task->d.task_app_v.cont, ncont);
	return task;
      }
    case FUNCTION_F:
      {
	struct function_s *val = new_function ();
	struct task_s *task = new_task ();
	struct continuation_s *ncont = new_continuation ();

	val->t = FUNCTION_DCONT;
	init_ptr (&val->d.function_cont_v, cont);
	task->t = TASK_APP;
	ncont->t = CONTINUATION_ABORT;
	init_ptr (&task->d.task_app_v.erator, rand);
	init_ptr (&task->d.task_app_v.erand, val);
	init_ptr (&task->d.task_app_v.cont, ncont);
	return task;
      }
    case FUNCTION_E:
      {
	struct task_s *task = new_task ();

	task->t = TASK_FINAL;
	return task;
      }
    case FUNCTION_AT:
      {
	struct function_s *val = new_function ();
	struct task_s *task = new_task ();

	current_ch = getchar ();
	val->t = (current_ch != EOF ? FUNCTION_I : FUNCTION_V);
	task->t = TASK_APP;
	init_ptr (&task->d.task_app_v.erator, rand);
	init_ptr (&task->d.task_app_v.erand, val);
	init_ptr (&task->d.task_app_v.cont, cont);
#if 0  /* Harmless but not necessary */
	free_function (val);
#endif
	return task;
      }
    case FUNCTION_QUES:
      {
	struct function_s *val = new_function ();
	struct task_s *task = new_task ();

	val->t = (current_ch == rator->d.function_ques_v
		  ? FUNCTION_I : FUNCTION_V);
	task->t = TASK_APP;
	init_ptr (&task->d.task_app_v.erator, rand);
	init_ptr (&task->d.task_app_v.erand, val);
	init_ptr (&task->d.task_app_v.cont, cont);
#if 0  /* Harmless but not necessary */
	free_function (val);
#endif
	return task;
      }
    case FUNCTION_PIPE:
      {
	struct function_s *val = new_function ();
	struct task_s *task = new_task ();

	if ( current_ch != EOF )
	  {
	    val->t = FUNCTION_DOT;
	    val->d.function_dot_v = current_ch;
	  }
	else
	  val->t = FUNCTION_V;
	task->t = TASK_APP;
	init_ptr (&task->d.task_app_v.erator, rand);
	init_ptr (&task->d.task_app_v.erand, val);
	init_ptr (&task->d.task_app_v.cont, cont);
#if 0  /* Harmless but not necessary */
	free_function (val);
#endif
	return task;
      }
    }
  fprintf (stderr, "INTERNAL ERROR: apply() surprised!\n");
  return NULL;
}
Ejemplo n.º 8
0
Archivo: bfcc.c Proyecto: germuth/bfcc
int main(int argc, char **argv){
  if(argc <= 0){
    return -1;
  }
  /*
  char* filename = argv[1];
  char* ssss;
  ssss = filename;


  */
  FILE *pInputFile;
  //pInputFile = fopen(filename, "r");
  pInputFile = fopen("input/test.bfc", "r");
  //fp = fopen("/etc/vim/vimrc", "r");
  if (pInputFile == NULL){
    exit(EXIT_FAILURE);
  }

  FILE *pOutputFile;
  pOutputFile = fopen("output/out.asm", "w");

  cstring* data;
  data = (cstring*) malloc(sizeof(cstring) * 1000);
  int dataIdx = 0;
  cstring* text;
  text = (cstring*) malloc(sizeof(cstring) * 1000);
  int textIdx = 0;

  int levelsDeep = 0;

  char* line = (char*) malloc(sizeof(char) * MAX_LINE_LENGTH);
  //array of 15 pointers
  //char** tokens = (char**) malloc(15);
  tokens = (token*) malloc(sizeof(token) * 15);
  currToken = 0;

  //char** functionStack = (char**) malloc(50);
  //char* pNull = "NULL";
  cstring* functionStack = (cstring*) malloc(sizeof(cstring) * 50);

  functionStack = new_cstring(10);
  functionStack->i = "NULL";
  functionStack++;

  //TODO no point in going line by line anymore, just grab tokens...
  while(fgets(line, MAX_LINE_LENGTH, pInputFile)){
    token* token = getNextToken(&line);
    while(token != NULL){
      printf("%s\n", token->str.i);
      //handle token
      //remove brackets, semicolons, etc
      //clean(&(token->str));
      tokens[currToken++] = *token;

      token = getNextToken(&line);
    }
  }

  determineTokenType();

  //should combine tokens into expressions, ex
  //char* my_str; is four tokens but one 'expression'
  //printf("hello world");
  //is two expressions, expression has return value
  //but still need expression tree

  //this is so messy my god help
  tree* expTrees = new (tree*) malloc(sizeof(tree) * MAX_NUM_LINES);
  tree* currTree = expTrees;

  token* currLine = *tokenLines;
  int i;
  for(i = 0; i < currLine; i++){
    if(currTree == NULL){
      expression* root = new_expression();
      root->type = ROOT;
      currTree = new_tree(root);
    }
    token curr = *currLine;
    
    if(curr.type == TYPE){
      //this and next are expression
      //recursive procedure to attach next tokens as expression to current tree
      //this way we can have func( x+y, func2(y*z) )
      //if we need to attach root expression node, perhaps each line should root
      //node
    }
  }

  fclose(pInputFile);
  exit(EXIT_SUCCESS);
}