예제 #1
0
static void parseapplication(void)
{
  if(tokentype == TYPEID)
  {
    int count = 1;
    push(gettemplate(tokenval));
    gettoken();
    while(tokentype == NUMBER
       || tokentype == IDENTIFIER
       || tokentype == TYPEID
       || tokentype == CHARACTER
       || tokentype == STRING
       || tokentype == LPAR
       || tokentype == LBRACK
       || tokentype == LACC)
    {
      parseterm();
      count++;
    }
    makecompound(STRUCT, count);
  }
  else if(tokentype == OPERATOR)
    parsename();
  else
    parseterm();
  while(tokentype == NUMBER
     || tokentype == IDENTIFIER
     || tokentype == TYPEID
     || tokentype == CHARACTER
     || tokentype == STRING
     || tokentype == LPAR
     || tokentype == LBRACK
     || tokentype == LACC)
  {
    parseterm();
    makeinverse(APPLY);
  }
  if(tokentype == OPERATOR && strcmp(tokenval, ":") == 0)
  {
    gettoken();
    if(tokentype == RPAR)
    {
      push(gettemplate(":"));
      make(APPLY);
    }
    else
    {
      parseapplication();
      makeinverse(LIST);
    }
  }
}
예제 #2
0
static void parsename(void)
{
  if(strcmp(tokenval, "-") == 0)
    push(gettemplate("neg"));
  else if(strcmp(tokenval, "num") == 0)
    makeconstant(MATCHTYPE, INT);
  else if(strcmp(tokenval, "bool") == 0)
    makeconstant(MATCHTYPE, BOOLEAN);
  else if(strcmp(tokenval, "char") == 0)
    makeconstant(MATCHTYPE, CHAR);
  else
    push(gettemplate(tokenval));
  gettoken();
}
예제 #3
0
static void parsesection(int prio)
{
  while(tokentype == OPERATOR)
  {
    Cell *temp  = gettemplate(tokenval);
    FuncDef *fun = getfunction(temp->value);

    if(fun->prio > prio) break;
    push(temp);
    make(APPLY);
    gettoken();
    if(tokentype == RPAR) break;
    parseexpression(fun->assoc==Left ? fun->prio-1 : fun->prio);
    makeinverse(APPLY);
  }
}
예제 #4
0
bool Load(const char *filename)
{
  static char fileName[stringsize] = "";
  if(filename) strncat(strcpy(fileName, ""), filename, stringsize-1);
  if(strlen(fileName) == 0)
    return False;
  else if(seterror()==0)
  {
    initstack();
    unlockmem();
    inithashtable();
    lockmem();
    initlex();
    initlib();
    initsyslib();
    initmodify();
    parsefile(inipath);
    parsefile(fileName);
    checkdefinitions();
    modify_definitions();
    lockmem();
    if (gettemplate("main")->tag == FUNC)
    {
      Interpret("main");
    }
    return True;
  }
  else
  {
    initstack();
    unlockmem();
    inithashtable();
    lockmem();
    initlex();
    initlib();
    initsyslib();
    initmodify();
    parsefile(inipath);
    checkdefinitions();
    modify_definitions();
    lockmem();
    return False;
  }
}
예제 #5
0
int main(int argc, char * argv[])
{
    char filename[256];
	int i =0;
	
	int num;
	int iNum;
	int oNum;
	
	FILE *fp;
	
	fp = fopen("./train/new_feature_train.set","r");
	if (fp == NULL){
	    printf("can't open new_feature_train.net\n");
		return 1;
	}
	
	gettemplateInfo(fp,&num,&iNum,&oNum);
	gettemplate(fp,num,iNum,oNum);
	
	printf("num=%d,iNum=%d,oNum=%d\n",num,iNum,oNum);
	
	
	
	if (argc < 2){
	    printf("Usage:\n");
		printf(" ./bmp file_to_recog\n");
		exit(0);
	}
	else{
	    for (i=1; i<argc; i++){
            strcpy(filename, argv[i]);
            printf("%s\n",filename);
            recogBMP(filename);
		}
    }
    return 0;
}
예제 #6
0
int CreateRemote(char s[])
{
  if(!RemoteOk) return -1;
  if(seterror() == 0)
  {
    Cell *pair;
    int k;
    buildstring(s);
    push(gettemplate("object"));
    make(APPLY);
    eval();
    pair = pop();
    push(template_nil);
    push(template_nil);
    push(pair->left);
    push(pair->right->left);
    push(template_nil);
    makeinverse(LIST);
    makeinverse(LIST);
    makeinverse(LIST);
    makeinverse(LIST);
    for(k=0; k < ObjectCount; k++)
      if(stack[k]->tag != LIST)
      {
        stack[k]= pop();
        return k;
      }
    return ObjectCount++;
  }
  else
  {
    setstackheight(ObjectCount);
    interrupted = False;
    return -1;
  }
}
예제 #7
0
/********************************************************************
  initialisation of hashtable with system functions
*********************************************************************/
static void initsyslib(void)
{
  Cell *obj = gettemplate("objecttype");
  inserttypestring("object", "[char] -> objecttype");
  insertabstype("object", obj);
  parsetypeexpr("(*, [char] -> [[char]] -> * -> (*, [[char]]))");
  makeconstant(FUNC, obj->value);
  makecompound(STRUCT, 1);
  make(TYPESYNONYM);
  inserttypeexpr("objecttype", pop());
  insertabstype("objecttype", obj);

  insert("_section", 3, FUNC      , NULL, apply_SECTION);
  insert("if"      , 3, FUNC      , NULL, applyIF);
  insert("^"       , 2, FUNC      , NULL, applyPOWER);
  insert("neg"     , 1, FUNC      , NULL, applyNEG);
  insert("*"       , 2, FUNC      , NULL, applyTIMES);
  insert("/"       , 2, FUNC      , NULL, applyDIV);
  insert("//"      , 2, FUNC      , NULL, applyDIVIDE);
  insert("%"       , 2, FUNC      , NULL, applyMOD);
  insert("+"       , 2, FUNC      , NULL, applyPLUS);
  insert("-"       , 2, FUNC      , NULL, applyMINUS);
  insert("="       , 2, FUNC      , NULL, applyEQ);
  insert("~="      , 2, FUNC      , NULL, applyNE);
  insert("<"       , 2, FUNC      , NULL, applyLT);
  insert("<="      , 2, FUNC      , NULL, applyLE);
  insert(">"       , 2, FUNC      , NULL, applyGT);
  insert(">="      , 2, FUNC      , NULL, applyGE);
  insert("&"       , 2, FUNC      , NULL, applyUPDATE);
  insert("True"    , 0, BOOLEAN   , NULL, NULL);
  insert("False"   , 0, BOOLEAN   , NULL, NULL);
  insert("pi"      , 0, REAL      , NULL, NULL);
  insert("Nil"     , 0, NIL       , NULL, NULL);
  insert(""        , 1, FUNC      , NULL, NULL);
  insert("strict"  , 2, FUNC      , NULL, applySTRICT);

  inserttypestring("_section"  , "(* -> ** -> ***) -> ** -> * -> ***");
  inserttypestring("if"        , "bool -> * -> * -> *");
  inserttypestring("^"         , "num -> num -> num");
  inserttypestring("neg"       , "num -> num");
  inserttypestring("*"         , "num -> num -> num");
  inserttypestring("/"         , "num -> num -> num");
  inserttypestring("//"        , "num -> num -> num");
  inserttypestring("%"         , "num -> num -> num");
  inserttypestring("+"         , "num -> num -> num");
  inserttypestring("-"         , "num -> num -> num");
  inserttypestring("="         , "* -> * -> bool");
  inserttypestring("~="        , "* -> * -> bool");
  inserttypestring("<"         , "* -> * -> bool");
  inserttypestring("<="        , "* -> * -> bool");
  inserttypestring(">"         , "* -> * -> bool");
  inserttypestring(">="        , "* -> * -> bool");
  inserttypestring("&"         , "* -> * -> *");
  inserttypestring("True"      , "bool");
  inserttypestring("False"     , "bool");
  inserttypestring("pi"        , "num");
  inserttypestring("Nil"       , "[*]");
  inserttypestring("strict"    , "(* -> **) -> * -> **");

  insertsys("strict");
  insertsys("^");
  insertsys("neg");
  insertsys("*");
  insertsys("/");
  insertsys("//");
  insertsys("%");
  insertsys("+");
  insertsys("-");
  insertsys("=");
  insertsys("~=");
  insertsys("<");
  insertsys("<=");
  insertsys(">");
  insertsys(">=");

  insertoperator("."   , 1, Right);
  insertoperator(":"   , 1, Right);
  insertoperator("&"   , 1, Left);
  insertoperator("!"   , 2, Left);
  insertoperator("^"   , 2, Right);
  insertoperator("*"   , 3, Left);
  insertoperator("/"   , 3, Left);
  insertoperator("//"  , 3, Left);
  insertoperator("%"   , 3, Left);
  insertoperator("++"  , 4, Right);
  insertoperator("--"  , 4, Left);
  insertoperator("+"   , 4, Left);
  insertoperator("-"   , 4, Left);
  insertoperator("="   , 5, Right);
  insertoperator("~="  , 5, Left);
  insertoperator("<"   , 5, Left);
  insertoperator("<="  , 5, Left);
  insertoperator(">"   , 5, Left);
  insertoperator(">="  , 5, Left);
  insertoperator("/\\" , 6, Right);
  insertoperator("\\/" , 7, Right);

  template_divide = gettemplate("//");
  template_div    = gettemplate("/");
  template_mod    = gettemplate("%");
  template_power  = gettemplate("^");
  template_update = gettemplate("&");
}
예제 #8
0
static void parsestructdef(void)
{
  char structname[stringsize];
  char *headname;
  int count;
  Cell *head = pop();

  setchecktypevariables(COLLECT);
  push(template_match);
  for(; head->tag==APPLY; head=head->left)
  {
    if(head->right->tag != UNDEFINED && head->right->tag != FUNC) parseerror(9);
    push(maketypevariable(getfunction(head->right->value)->name));
    make(STRUCT);
  }
  if(head->tag != UNDEFINED && head->tag != FUNC) parseerror(10);
  headname = getfunction(head->value)->name;
  makeconstant(FUNC, head->value);
  make(STRUCT);
  setchecktypevariables(CHECK);
  gettoken();
  head = top();
  if(tokentype == LACC)
  {
    count = 0;
    do
    {
      gettoken();
      if(tokentype != IDENTIFIER) parseerror(25);
      push(gettemplate(tokenval));
      gettoken();
      if(tokentype != COLONS) parseerror(15);
      gettoken();
      parsetype(TYPEEXPR);
      makerecordfield(head, getN(2), getN(1));
      makeinverse(TYPEDEF);
      count++;
    }
    while(tokentype == COMMA);
    makecompound(RECORD, count);
    makeinverse(TYPEDEF);
    if(tokentype != RACC) parseerror(33);
    gettoken();
  }
  else
  {
    for(;;)
    {
      if(tokentype != TYPEID) parseerror(11);
      strcpy(structname, tokenval);
      count = 0;
      gettoken();
      while(tokentype == IDENTIFIER
         || tokentype == OPERATOR
         || tokentype == LBRACK
         || tokentype == LPAR)
      {
        parsetype(TYPETERM);
        count++;
      }
      push(head);
      while(count-- > 0) makeinverse(APPLY);
      if(!inserttypeexpr(structname, pop())) parseerror(12);
      if(tokentype != BAR) break;
      gettoken();
    }
  }
  if(!inserttypeexpr(headname, pop())) parseerror(12);
  setchecktypevariables(NOCHECK);
}
예제 #9
0
static void parsetype(TypeType typetype)
{
  switch(tokentype)
  {
    case IDENTIFIER:
      if(strcmp(tokenval, "num") == 0)
      {
        push(newcell(INT));
        gettoken();
      }
      else if(strcmp(tokenval, "char") == 0)
      {
        push(newcell(CHAR));
        gettoken();
      }
      else if(strcmp(tokenval, "bool") == 0)
      {
        push(newcell(BOOLEAN));
        gettoken();
      }
      else
      {
        int count = 1;
        push(gettemplate(tokenval));
        gettoken();
        if(typetype == TYPEEXPR)
          while(tokentype == IDENTIFIER
             || tokentype == OPERATOR
             || tokentype == LBRACK
             || tokentype == LPAR)
          {
            parsetype(TYPETERM);
            count++;
          }
        makecompound(STRUCT, count);
      }
      break;
    case OPERATOR:
      push(maketypevariable(tokenval));
      gettoken();
      break;
    case LPAR:
      gettoken();
      if(tokentype == RPAR)
        push(newcell(NULLTUPLE));
      else
      {
        parsetype(TYPEEXPR);
        if(tokentype == COMMA)
        {
          int count = 1;
          while(tokentype == COMMA)
          {
            gettoken();
            parsetype(TYPEEXPR);
            count++;
          }
          makecompound(PAIR, count);
        }
      }
      if(tokentype != RPAR) parseerror(2);
      gettoken();
      break;
    case LBRACK:
      gettoken();
      parsetype(TYPEEXPR);
      push(template_nil);
      makeinverse(LIST);
      if(tokentype != RBRACK) parseerror(1);
      gettoken();
      break;
    default:
      parseerror(8);
  }
  if(typetype == TYPEEXPR && tokentype == ARROW)
  {
    gettoken();
    parsetype(TYPEEXPR);
    makeinverse(APPLY);
  }
}
예제 #10
0
static void parseterm(void)
{
  int count;
  switch(tokentype)
  {
    case NUMBER:
      if(strchr(tokenval, '.') == NULL)
        makeINT(atol(tokenval));
      else
        makeREAL(atof(tokenval));
      gettoken();
      break;
    case IDENTIFIER:
      parsename();
      break;
    case TYPEID:
      push(gettemplate(tokenval));
      makecompound(STRUCT, 1);
      gettoken();
      break;
    case CHARACTER:
      makeconstant(CHAR, tokenval[0]);
      gettoken();
      break;
    case STRING:
      buildstring(tokenval);
      gettoken();
      break;
    case LPAR:
      gettoken();
      if(tokentype == OPERATOR && strcmp(tokenval, "-") != 0)
      {
        parsename();
        if(tokentype != RPAR)
        {
          parseexpression(MAXPRIO);
          rotatestack();
          push(gettemplate("_section"));
          make(APPLY);
          make(APPLY);
        }
      }
      else if(tokentype == RPAR)
        makeconstant(NULLTUPLE, 0);
      else
      {
        parseexpression(MAXPRIO);
        if(tokentype == COMMA)
        {
          count = 1;
          while(tokentype == COMMA)
          {
            gettoken();
            parseexpression(MAXPRIO);
            count++;
          }
          makecompound(PAIR, count);
        }
      }
      if(tokentype != RPAR) parseerror(2);
      gettoken();
      break;
    case LBRACK:
      parselist();
      break;
    case LACC:
      count = 0;
      do
      {
        gettoken();
        if(tokentype != IDENTIFIER) parseerror(25);
        push(gettemplate(tokenval));
        gettoken();
        if(strcmp(tokenval, "=") != 0) parseerror(5);
        gettoken();
        parseexpression(MAXPRIO);
        makeinverse(ALIAS);
        count++;
      }
      while(tokentype == COMMA);
      makecompound(RECORD, count);
      if(tokentype != RACC) parseerror(33);
      gettoken();
      break;
    default:
      parseerror(3);
  }
}
예제 #11
0
static void parselist(void)
{
  int count = 0;

  gettoken();
  if(tokentype != RBRACK)
  {
    parseexpression(MAXPRIO);
    count++;
  }
  while(tokentype == COMMA)
  {
    gettoken();
    parseexpression(MAXPRIO);
    count++;
  }
  if(tokentype == RBRACK)
  {
    push(template_nil);
    while(count-->0) makeinverse(LIST);
  }
  else if(tokentype == BAR && count >= 1)
  {
    push(template_nil);
    while(count-->0) makeinverse(LIST);
    count = 1;
    gettoken();
    parsegenerators(&count);
    push(template_nil);
    while(count-->0) makeinverse(GENERATOR);
  }
  else if(tokentype == POINTS && count == 1)
  {
    gettoken();
    if(tokentype == RBRACK)
    {
      push(gettemplate("nats"));
      make(APPLY);
    }
    else
    {
      push(gettemplate("nat"));
      make(APPLY);
      parseexpression(MAXPRIO);
      makeinverse(APPLY);
    }
  }
  else if(tokentype == POINTS && count == 2)
  {
    gettoken();
    if(tokentype == RBRACK)
    {
      rotatestack();
      push(gettemplate("gennats"));
      make(APPLY);
      make(APPLY);
    }
    else
    {
      rotatestack();
      push(gettemplate("gennat"));
      make(APPLY);
      make(APPLY);
      parseexpression(MAXPRIO);
      makeinverse(APPLY);
    }
  }
  if(tokentype != RBRACK) parseerror(1);
  gettoken();
}