Ejemplo n.º 1
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();
}
Ejemplo n.º 2
0
static void applyDIVIDE(void)
{
  Real x = evalreal(getN(1)), y = evalreal(getN(2));
  if(y==0)
    makeconstant(ERROR, template_divide->value);
  else
    makeREAL(x/y);
}
Ejemplo n.º 3
0
static void applyMOD(void)
{
  Integer x = evalint(getN(1), template_mod->value);
  Integer y = evalint(getN(2), template_mod->value);
  if(y==0)
    makeconstant(ERROR, template_mod->value);
  else
    makeINT(x%y);
}
Ejemplo n.º 4
0
static void applyDIV(void)
{
  Integer x = evalint(getN(1), template_div->value);
  Integer y = evalint(getN(2), template_div->value);
  if(y==0)
    makeconstant(ERROR, template_div->value);
  else
    makeINT(x/y);
}
Ejemplo n.º 5
0
static void buildstring(char *s)
{
  int count = 0;
  for(;*s != '\0'; s++)
  {
    makeconstant(CHAR, *s);
    count++;
  }
  push(template_nil);
  while(count-->0) makeinverse(LIST);
}
Ejemplo n.º 6
0
static void parsetypesynonym(void)
{
  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);
  makeconstant(FUNC, head->value);
  make(STRUCT);
  setchecktypevariables(CHECK);
  gettoken();
  parsetype(TYPEEXPR);
  makeinverse(TYPESYNONYM);
  if(!inserttypeexpr(getfunction(head->value)->name, pop())) parseerror(12);
  setchecktypevariables(NOCHECK);
}
Ejemplo n.º 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("&");
}
Ejemplo n.º 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);
}
Ejemplo n.º 9
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);
  }
}