示例#1
0
文件: parser.c 项目: adamkao/citrine
/**
 * CTRParserExpression
 *
 * Generates a set of nodes to represent an expression.
 */
ctr_tnode* ctr_cparse_expr(int mode) {
	ctr_tnode* r;
	ctr_tnode* e;
	int t2;
	ctr_tlistitem* nodes;
	ctr_tlistitem* rli;
	if (ctr_mode_debug) printf("Parsing expression (mode: %d).\n", mode);	
	r = ctr_cparse_receiver();
	t2 = ctr_clex_tok();
	if (ctr_mode_debug) printf("First token after receiver = %d \n", t2);
	ctr_clex_putback();
	if (r->type == CTR_AST_NODE_REFERENCE && t2 == CTR_TOKEN_ASSIGNMENT) {
		e = ctr_cparse_assignment(r);
	} else if (t2 != CTR_TOKEN_DOT && t2 != CTR_TOKEN_PARCLOSE && t2 != CTR_TOKEN_CHAIN) {
		e = CTR_PARSER_CREATE_NODE();
		e->type = CTR_AST_NODE_EXPRMESSAGE;
		nodes = ctr_cparse_messages(r, mode);
		if (nodes == NULL) {
			int t = ctr_clex_tok();
			ctr_clex_putback();
			if (ctr_mode_debug) printf("No messages, return. Next token: %d.\n", t);
			return r; /* no messages, then just return receiver (might be in case of argument). */
		}
		rli = CTR_PARSER_CREATE_LISTITEM();
		rli->node = r;
		rli->next = nodes;
		e->nodes = rli;
	} else {
		if (ctr_mode_debug) printf("Returning receiver. \n");
		return r;
	}
	return e;
}
示例#2
0
文件: parser.c 项目: probonopd/citron
ctr_tnode *
ctr_cparse_expr (int mode)
{
  ctr_tnode *r;
  ctr_tnode *e;
  int t2;
  ctr_tlistitem *nodes;
  ctr_tlistitem *rli;
  struct lexer_state lexer_state;
  ctr_clex_dump_state (&lexer_state);
  r = ctr_cparse_receiver ();
  t2 = ctr_clex_tok ();
  ctr_clex_putback ();

  /* user tries to put colon directly after recipient */
  if (t2 == CTR_TOKEN_COLON)
    {
      /* Parse as if we had a "me" before this */
      ctr_clex_load_state (lexer_state);
      if (ctr_clex_inject_token (CTR_TOKEN_REF, me_s, 2))
	ctr_cparse_emit_error_unexpected (t2, "Recipient cannot be followed by a colon in this state.\n");
      return ctr_cparse_expr (mode);
    }

  if (t2 == CTR_TOKEN_ASSIGNMENT)
    {
      // if ( r->type != CTR_AST_NODE_REFERENCE ) {
      //     ctr_cparse_emit_error_unexpected( t2, "Invalid left-hand assignment.\n" );
      //     exit(1);
      // }
      e = ctr_cparse_assignment (r);
    }
  else if (t2 == CTR_TOKEN_PASSIGNMENT)
    {
      if (r->type != CTR_AST_NODE_REFERENCE)
	{
	  ctr_cparse_emit_error_unexpected (t2, "Invalid left-hand assignment.\n");
	  exit (1);
	}
      r->modifier = 1;		//set private */
      e = ctr_cparse_assignment (r);	//go as usual
      /*r->nodes->node->type = CTR_AST_NODE_LTRSTRING;
         e = ctr_cparse_create_node(CTR_AST_NODE);
         e->type = CTR_AST_NODE_EXPRMESSAGE;
         e->nodes = ctr_heap_allocate(sizeof(ctr_tlistitem));
         e->nodes->node = ctr_cparse_create_node(CTR_AST_NODE);
         e->nodes->node->value = ctr_heap_allocate(sizeof(char)*7); //Reflect
         memcpy(e->nodes->node->value, "Reflect", 7);
         e->nodes->node->vlen = 7;
         e->nodes->node->type = CTR_AST_NODE_REFERENCE;
         e->nodes->next = ctr_heap_allocate(sizeof(ctr_tlistitem));
         e->nodes->next->node = ctr_cparse_create_node(CTR_AST_NODE);
         e->nodes->next->node->type = CTR_AST_NODE_KWMESSAGE;
         e->nodes->next->node->value = ctr_heap_allocate(sizeof(char)*7); //set:to:
         memcpy(e->nodes->next->node->value, "set:to:", 7);
         e->nodes->next->node->vlen = 7;
         e->nodes->next->node->nodes = ctr_heap_allocate(sizeof(ctr_tlistitem));
         e->nodes->next->node->nodes->node = r->nodes->node;
         e->nodes->next->node->nodes->next = ctr_heap_allocate(sizeof(ctr_tlistitem));
         e->nodes->next->node->nodes->next->node = r->nodes->next->node; */
    }
  else if (t2 != CTR_TOKEN_DOT && t2 != CTR_TOKEN_PARCLOSE && (t2 != CTR_TOKEN_CHAIN && mode != CTR_AST_NODE_IMMUTABLE))
    {
      e = ctr_cparse_create_node (CTR_AST_NODE);
      e->type = CTR_AST_NODE_EXPRMESSAGE;
      nodes = ctr_cparse_messages (r, mode == CTR_AST_NODE_IMMUTABLE ? 0 : mode);
      if (nodes == NULL)
	{
	  ctr_clex_tok ();
	  ctr_clex_putback ();
	  return r;		/* no messages, then just return receiver (might be in case of argument). */
	}
      rli = (ctr_tlistitem *) ctr_heap_allocate_tracked (sizeof (ctr_tlistitem));
      rli->node = r;
      rli->next = nodes;
      e->nodes = rli;
    }
  else
    {
      return r;
    }
  return e;
}