示例#1
0
//-----------------------------------------------------------------------------
// Function: IPXactValueParser::baseForExpression()
//-----------------------------------------------------------------------------
int IPXactValueParser::baseForExpression(QString const& expression) const
{
    QString format = getFormatForExpression(expression);
    if (format == "bitString")
    {
        return 2;
    }
    else if (format == "long")
    {
        if (isHexadecimal(expression))
        {
            return 16;
        }
        else
        {
            return 10;
        }
    }
    else if (format == "float")
    {
        return 10;
    }

    return 0;
}
示例#2
0
文件: eval4.c 项目: jbulow/maru-3
static int readChar(FILE *fp)
{
    int c= getc(fp);
    if ('\\' == c) {
        c= getc(fp);
        switch (c) {
        case 'a':
            return '\a';
        case 'b':
            return '\b';
        case 'f':
            return '\f';
        case 'n':
            return '\n';
        case 'r':
            return '\r';
        case 't':
            return '\t';
        case 'v':
            return '\v';
        case '\'':
            return '\'';
        case '"':
            return '"';
        case '\\':
            return '\\';
        case 'u': {
            int a= getc(fp), b= getc(fp), c= getc(fp), d= getc(fp);
            return (digitValue(a) << 24) + (digitValue(b) << 16) + (digitValue(c) << 8) + digitValue(d);
        }
        case 'x': {
            int x= 0;
            while (isHexadecimal(c= getc(fp)))
                x= x * 16 + digitValue(c);
            ungetc(c, fp);
            return x;
        }
        case '0' ... '7': {
            int x= 0;
            if (isOctal(c= getc(fp))) {
                x= x * 8 + digitValue(c);
                if (isOctal(c= getc(fp))) {
                    x= x * 8 + digitValue(c);
                    if (isOctal(c= getc(fp))) {
                        x= x * 8 + digitValue(c);
                        c= getc(fp);
                    }
                }
            }
            ungetc(c, fp);
            return x;
        }
        default:
            fprintf(stderr, "illegal character escape: \\%c", c);
            exit(1);
            break;
        }
    }
示例#3
0
文件: hex.c 项目: rmobis/MC404
bool validateHexOpType(const char *token) {
	if (!isHexadecimal(token)) {
		return false;
	}

	long long v = strtoll(token, NULL, 16);

	// Even though hex values actually go from -2^39 to (2^39 - 1), since we're
	// considering a 64 bits data type, the representation gets slightly
	// different.
	return v >= 0 && v <= 1099511627775LL;
}
示例#4
0
//-----------------------------------------------------------------------------
// Function: IPXactValueParser::longValueOf()
//-----------------------------------------------------------------------------
qreal IPXactValueParser::longValueOf(QString const& value) const
{
    if (isHexadecimal(value))
    {
        QString hexValue = value;
        return hexValue.remove('#').toLong(0, 16);
    }
    else if (value.endsWith('k', Qt::CaseInsensitive))
    {
        QString coefficient = value;
        coefficient.chop(1);

        return coefficient.toLong()*1024;
    }
    else if (value.endsWith('M', Qt::CaseInsensitive))
    {
        QString coefficient = value;
        coefficient.chop(1);

        return coefficient.toLong()*1024*1024;
    }
    else if (value.endsWith('G', Qt::CaseInsensitive))
    {
        QString coefficient = value;
        coefficient.chop(1);

        return coefficient.toLong()*1024*1024*1024;
    }
    else if (value.endsWith('T', Qt::CaseInsensitive))
    {
        QString coefficient = value;
        coefficient.chop(1);

        return coefficient.toLong()*1024*1024*1024*1024;
    }
    else
    {
        return value.toLong();
    }
}
示例#5
0
BOOL CPackage::checkSignature()
{
	CString csFile,
			csDigest,
			csRegDigest;
	BOOL	bRes;

	// Read meta data digest in the registry and verify it
	if (!regReadPackageDigest( csRegDigest))
		// No digest registered in Registry
		return FALSE;
	csFile.Format( _T( "%s\\%s\\%s"), getDownloadFolder(), m_csID, OCS_DOWNLOAD_METADATA);
	if (!fileDigest( csFile, csDigest))
		// Unable to compute digest on meta data
		return FALSE;
	if (csRegDigest.Compare( csDigest) != 0)
		// Digest registered in Registry is not the same
		return FALSE;
	// Now, check ZIP digest
	if (m_uFrags > 0)
	{
		// Compute digest on build.zip file
		csFile.Format( _T( "%s\\%s\\%s"), getDownloadFolder(), m_csID, OCS_DOWNLOAD_BUILD);
		if (isBase64())
			bRes = fileDigest( csFile, csDigest, m_csDigestAlgo);
		else if (isHexadecimal())
			bRes = fileDigest( csFile, csDigest, m_csDigestAlgo, FALSE);
		else
			// Unsupported encoding
			return FALSE;
		// Digest compute successfully
		if (bRes && (m_csDigest.Compare( csDigest) == 0))
			return TRUE;
		else
			return FALSE;
	}
	// No ZIP file
	return TRUE;
}
示例#6
0
int main()
{

	int start_loc = 0, locctr = 0 , end_loc, program_length = 0, instr_length = 0;
	char program_name[8] = {'\0'}, filename[] = "fig2.5.txt";
	
	FILE *fptr;
	
	char accept[100] = {'\0'};
   	char label[30], opcode[30], operand[30];

	int A = 0, X = 0, L = 0,PC = 0, SW = 0, B = 0, S = 0, T = 0, F = 0;
	int n = 1, i = 1, x = 0, b = 0, p = 0, e = 0, address = 0;

/************************************ pass 1 ****************************************/  	
	if((fptr = fopen(filename, "r")) != NULL)
	{
        //printf("file found\n");
        
        fgets(accept, 30, fptr);
		read(label, opcode, operand, accept);

        if(strcmp(opcode,"START") == 0)
        {
			int k, six = 1, hvalue = 0;
			strcpy(program_name, label);
			if(isHexadecimal(operand) == 1)
			{
				for(k = strlen(operand) - 1, six ; k >= 0 ; k--, six *= 16)
				{
					int temp = (int)operand[k] - 48;
					if(temp > 9)
					{
						temp = 10 + (int)operand[k] - 65;
					}

					hvalue += temp * six;
				}
				start_loc = hvalue;
				locctr = hvalue;
			}
			else
			{
				sscanf(operand, "%d", &start_loc);
				sscanf(operand, "%d", &locctr);
			}
        }
		else
		{
			locctr = 0;
		}

		fgets(accept, 30, fptr);
		read(label, opcode, operand, accept);


        while(!feof(fptr) && strcmp(opcode,"END") != 0)
        {
            if(isComment(accept) == 0) // not a comment
            {
				if(strlen(label) != 0)
				{
					if(inSymbol(label) != -1)
					{
						printf("error, duplicate symbol !!");
						return 0;
					}
					else
					{
						strcpy(SymbolTable[STindex].label, label);
						SymbolTable[STindex++].address = locctr; 
					}
				}

				if(isInstruction(opcode, "FORMAT") != 0)
				{
					if(opcode[0] == '+')
					{
						instr_length = 4;
					}
					else
					{
						instr_length = isInstruction(opcode, "FORMAT");
					}
				}
				else if(strcmp(opcode,"WORD") == 0)
				{
					instr_length = 3;
				}
				else if(strcmp(opcode,"RESW") == 0)
				{
					instr_length = 3 * ctoi(operand, "RESW");
				}
				else if(strcmp(opcode,"RESB") == 0)
				{
					instr_length = ctoi(operand, "RESB");
				}
				else if(strcmp(opcode,"BYTE") == 0)
				{
					instr_length = ctoi(operand, "BYTE");
				}
				else if(strcmp(opcode,"BASE") == 0)
				{
					instr_length = 0;
					int bp = inSymbol(operand);
					B = bp;
				}
				else if(strcmp(opcode,"END") == 0)
				{
					instr_length = 0;
				}
				else
				{
					printf("error, invaild operation code !!");
					return 0;
				}
            }

			locctr += instr_length;
			instr_length = 0;
			fgets(accept, 30, fptr);
			read(label, opcode, operand, accept);
        }

		end_loc = locctr;
		program_length =  end_loc - start_loc;
		fclose(fptr);
    }
    else
    {
        printf("file not found\n");
    }
/************************************ pass 2 ****************************************//*H T E M*/

	locctr = 0;
	instr_length = 0;

	if((fptr = fopen(filename, "r")) != NULL)
	{
        //printf("file found\n");

        fgets(accept, 30, fptr);
		read(label, opcode, operand, accept);

        if(strcmp(opcode,"START") == 0)
        {
			int k, six = 1, hvalue = 0;
			strcpy(program_name, label);
			if(isHexadecimal(operand) == 1)
			{
				for(k = strlen(operand) - 1, six ; k >= 0 ; k--, six *= 16)
				{
					int temp = (int)operand[k] - 48;
					if(temp > 9)
					{
						temp = 10 + (int)operand[k] - 65;
					}

					hvalue += temp * six;
				}
				locctr = hvalue;
			}
			else
			{
				sscanf(operand, "%d", &locctr);
			}
        }
		else
		{
			locctr = 0;
		}

		fgets(accept, 30, fptr);
		read(label, opcode, operand, accept);

		printf("H %-6s %06X %06X\n",program_name, start_loc, program_length);

        while(!feof(fptr) && strcmp(opcode,"END") != 0)
        {
			n = i = 1;
			x = b = p = e = 0;

			char object_code[50] = {'\0'};
            if(isComment(accept) == 0) // not a comment
            {
				if(strcmp(opcode,"WORD") == 0)
				{
					instr_length = 3;
					generate_objcode(object_code, operand, "WORD");
				}
				else if(strcmp(opcode,"RESW") == 0)
				{
					instr_length = 3 * ctoi(operand, "RESW");
				}
				else if(strcmp(opcode,"RESB") == 0)
				{
					instr_length = ctoi(operand, "RESB");
				}
				else if(strcmp(opcode,"BYTE") == 0)
				{
					instr_length = ctoi(operand, "BYTE");
					generate_objcode(object_code, operand, "BYTE");
				}
				else if(strcmp(opcode,"BASE") == 0)
				{
					instr_length = 0;
					noobj = 1;
				}
				else if(strcmp(opcode,"END") == 0)
				{
					instr_length = 0;
					noobj = 1;
				}
				else if(isInstruction(opcode, "FORMAT") != 0)
				{
					if(opcode[0] == '+')
					{			
						char temp[10] = {'\0'};
						strcpy(temp,strtok(operand,"#@"));
						if(inSymbol(temp) != -1)
						{
							Mrecord[Mindex++] = locctr + 1;  
						}

						instr_length = 4;
						e = 1;
						b = p = 0;
					}
					else
					{
						instr_length = isInstruction(opcode, "FORMAT");
					}
				}
				else
				{
					printf("error, invaild operation code !!");
					return 0;
				}
            }
/****************************************************************************************/			
			switch (operand[0])
			{
				case '@':
					n = 1; i = 0;
					break;
				case '#':
					n = 0; i = 1;
					break;
				default:
					n = i = 1;
					if(strstr(operand,",X")!=NULL)
					{
						x = 1;
						strtok(operand,",");
					}
					break;
			}

			if(inSymbol(operand) != -1)
			{
				b = 0; p = 1;
				if(opcode[0] == '+')
				{
					e = 1; b = p = 0;
				}
				address=inSymbol(operand);
			}
			else if(isdigit(operand[1])!=0)
			{
				sscanf(operand + 1,"%d",&address);
			}
			

			if(p == 1)
			{
				address = address - (locctr + instr_length);

				if(address > 2047 || address < -2048)
				{
					b = 1; p = 0;
					address = inSymbol(operand) - B;
				}
			}

			int format = isInstruction(opcode, "FORMAT"), op = isInstruction(opcode, "OPCODE");

			if(strcmp(opcode,"RSUB") == 0)
			{
				b = p = 0;
				address = 0;
			}
						
/******************************************************************************/
			char *head = (char*)malloc(4 * sizeof(char));
			if(format>=3)
			{
				op *= 16;	n *= 32;	i *= 16;	x *= 8;		b *= 4;		p *= 2;
				op += n + i + x + b + p + e;
				sprintf(object_code,"%03X",op);
			}
			else if(format==2)
				sprintf(object_code,"%02X",op);

			if(opcode[0]=='+')
				sprintf(object_code+3,"%05X",address);
			else if(format==3)
			{
				if(address<0)
				{
					sprintf(head,"%3hX",address);
					strcpy(object_code+3,head+1);
				}
				else
					sprintf(object_code+3,"%03X",address);
			}
			else if(format==2)
			{
				sprintf(object_code+2,"%1X",search_reg(operand[0]));
				if(strlen(operand) > 2)
					sprintf(object_code+3,"%1X",search_reg(operand[2]));
				else
					sprintf(object_code+3,"%1X",0);
			}

/*******************************************************************************/

			printf("T %06X %02X %s\n",locctr, strlen(object_code), object_code);

			locctr += instr_length;
			instr_length = 0;

			fgets(accept, 30, fptr);
			read(label, opcode, operand, accept);
        }
		printMrecord();
		printf("E %06X\n",start_loc);
		fclose(fptr);
    }
    else
    {
        printf("file not found\n");
    }

	//system("pause");
	return 0;	
}
示例#7
0
文件: jslex.c 项目: 8bitgeek/Espruino
void jslGetNextToken() {
  jslGetNextToken_start:
  // Skip whitespace
  while (isWhitespace(lex->currCh))
    jslGetNextCh();
  // Search for comments
  if (lex->currCh=='/') {
    // newline comments
    if (jslNextCh()=='/') {
      while (lex->currCh && lex->currCh!='\n') jslGetNextCh();
      jslGetNextCh();
      goto jslGetNextToken_start;
    }
    // block comments
    if (jslNextCh()=='*') {
      while (lex->currCh && !(lex->currCh=='*' && jslNextCh()=='/'))
        jslGetNextCh();
      if (!lex->currCh) {
        lex->tk = LEX_UNFINISHED_COMMENT;
        return; /* an unfinished multi-line comment. When in interactive console,
                   detect this and make sure we accept new lines */
      }
      jslGetNextCh();
      jslGetNextCh();
      goto jslGetNextToken_start;
    }
  }
  lex->tk = LEX_EOF;
  lex->tokenl = 0; // clear token string
  if (lex->tokenValue) {
    jsvUnLock(lex->tokenValue);
    lex->tokenValue = 0;
  }
  // record beginning of this token
  lex->tokenLastStart = jsvStringIteratorGetIndex(&lex->tokenStart.it) - 1;
  /* we don't lock here, because we know that the string itself will be locked
   * because of lex->sourceVar */
  lex->tokenStart.it = lex->it;
  lex->tokenStart.currCh = lex->currCh;
  // tokens
  if (((unsigned char)lex->currCh) < jslJumpTableStart ||
      ((unsigned char)lex->currCh) > jslJumpTableEnd) {
    // if unhandled by the jump table, just pass it through as a single character
    jslSingleChar();
  } else {
    switch(jslJumpTable[((unsigned char)lex->currCh) - jslJumpTableStart]) {
    case JSLJT_ID: {
      while (isAlpha(lex->currCh) || isNumeric(lex->currCh) || lex->currCh=='$') {
        jslTokenAppendChar(lex->currCh);
        jslGetNextCh();
      }
      lex->tk = LEX_ID;
      // We do fancy stuff here to reduce number of compares (hopefully GCC creates a jump table)
      switch (lex->token[0]) {
      case 'b': if (jslIsToken("break", 1)) lex->tk = LEX_R_BREAK;
      break;
      case 'c': if (jslIsToken("case", 1)) lex->tk = LEX_R_CASE;
      else if (jslIsToken("catch", 1)) lex->tk = LEX_R_CATCH;
      else if (jslIsToken("continue", 1)) lex->tk = LEX_R_CONTINUE;
      break;
      case 'd': if (jslIsToken("default", 1)) lex->tk = LEX_R_DEFAULT;
      else if (jslIsToken("delete", 1)) lex->tk = LEX_R_DELETE;
      else if (jslIsToken("do", 1)) lex->tk = LEX_R_DO;
      else if (jslIsToken("debugger", 1)) lex->tk = LEX_R_DEBUGGER;
      break;
      case 'e': if (jslIsToken("else", 1)) lex->tk = LEX_R_ELSE;
      break;
      case 'f': if (jslIsToken("false", 1)) lex->tk = LEX_R_FALSE;
      else if (jslIsToken("finally", 1)) lex->tk = LEX_R_FINALLY;
      else if (jslIsToken("for", 1)) lex->tk = LEX_R_FOR;
      else if (jslIsToken("function", 1)) lex->tk = LEX_R_FUNCTION;
      break;
      case 'i': if (jslIsToken("if", 1)) lex->tk = LEX_R_IF;
      else if (jslIsToken("in", 1)) lex->tk = LEX_R_IN;
      else if (jslIsToken("instanceof", 1)) lex->tk = LEX_R_INSTANCEOF;
      break;
      case 'n': if (jslIsToken("new", 1)) lex->tk = LEX_R_NEW;
      else if (jslIsToken("null", 1)) lex->tk = LEX_R_NULL;
      break;
      case 'r': if (jslIsToken("return", 1)) lex->tk = LEX_R_RETURN;
      break;
      case 's': if (jslIsToken("switch", 1)) lex->tk = LEX_R_SWITCH;
      break;
      case 't': if (jslIsToken("this", 1)) lex->tk = LEX_R_THIS;
      else if (jslIsToken("throw", 1)) lex->tk = LEX_R_THROW;
      else if (jslIsToken("true", 1)) lex->tk = LEX_R_TRUE;
      else if (jslIsToken("try", 1)) lex->tk = LEX_R_TRY;
      else if (jslIsToken("typeof", 1)) lex->tk = LEX_R_TYPEOF;
      break;
      case 'u': if (jslIsToken("undefined", 1)) lex->tk = LEX_R_UNDEFINED;
      break;
      case 'w': if (jslIsToken("while", 1)) lex->tk = LEX_R_WHILE;
      break;
      case 'v': if (jslIsToken("var", 1)) lex->tk = LEX_R_VAR;
      else if (jslIsToken("void", 1)) lex->tk = LEX_R_VOID;
      break;
      default: break;
      } break;
      case JSLJT_NUMBER: {
        // TODO: check numbers aren't the wrong format
        bool canBeFloating = true;
        if (lex->currCh=='.') {
          jslGetNextCh();
          if (isNumeric(lex->currCh)) {
            // it is a float
            lex->tk = LEX_FLOAT;
            jslTokenAppendChar('.');
          } else {
            // it wasn't a number after all
            lex->tk = '.';
            break;
          }
        } else {
          if (lex->currCh=='0') {
            jslTokenAppendChar(lex->currCh);
            jslGetNextCh();
            if ((lex->currCh=='x' || lex->currCh=='X') ||
                (lex->currCh=='b' || lex->currCh=='B') ||
                (lex->currCh=='o' || lex->currCh=='O')) {
              canBeFloating = false;
              jslTokenAppendChar(lex->currCh); jslGetNextCh();
            }
          }
          lex->tk = LEX_INT;
          while (isNumeric(lex->currCh) || (!canBeFloating && isHexadecimal(lex->currCh))) {
            jslTokenAppendChar(lex->currCh);
            jslGetNextCh();
          }
          if (canBeFloating && lex->currCh=='.') {
            lex->tk = LEX_FLOAT;
            jslTokenAppendChar('.');
            jslGetNextCh();
          }
        }
        // parse fractional part
        if (lex->tk == LEX_FLOAT) {
          while (isNumeric(lex->currCh)) {
            jslTokenAppendChar(lex->currCh);
            jslGetNextCh();
          }
        }
        // do fancy e-style floating point
        if (canBeFloating && (lex->currCh=='e'||lex->currCh=='E')) {
          lex->tk = LEX_FLOAT;
          jslTokenAppendChar(lex->currCh); jslGetNextCh();
          if (lex->currCh=='-' || lex->currCh=='+') { jslTokenAppendChar(lex->currCh); jslGetNextCh(); }
          while (isNumeric(lex->currCh)) {
            jslTokenAppendChar(lex->currCh); jslGetNextCh();
          }
        }
      } break;
      case JSLJT_STRING:
      {
        char delim = lex->currCh;
        lex->tokenValue = jsvNewFromEmptyString();
        if (!lex->tokenValue) {
          lex->tk = LEX_EOF;
          return;
        }
        JsvStringIterator it;
        jsvStringIteratorNew(&it, lex->tokenValue, 0);
        // strings...
        jslGetNextCh();
        while (lex->currCh && lex->currCh!=delim) {
          if (lex->currCh == '\\') {
            jslGetNextCh();
            char ch = lex->currCh;
            switch (lex->currCh) {
            case 'n'  : ch = 0x0A; jslGetNextCh(); break;
            case 'b'  : ch = 0x08; jslGetNextCh(); break;
            case 'f'  : ch = 0x0C; jslGetNextCh(); break;
            case 'r'  : ch = 0x0D; jslGetNextCh(); break;
            case 't'  : ch = 0x09; jslGetNextCh(); break;
            case 'v'  : ch = 0x0B; jslGetNextCh(); break;
            case 'u' :
            case 'x' : { // hex digits
              char buf[5] = "0x??";
              if (lex->currCh == 'u') {
                // We don't support unicode, so we just take the bottom 8 bits
                // of the unicode character
                jslGetNextCh();
                jslGetNextCh();
              }
              jslGetNextCh();
              buf[2] = lex->currCh; jslGetNextCh();
              buf[3] = lex->currCh; jslGetNextCh();
              ch = (char)stringToInt(buf);
            } break;
            default:
              if (lex->currCh>='0' && lex->currCh<='7') {
                // octal digits
                char buf[5] = "0";
                buf[1] = lex->currCh;
                int n=2;
                jslGetNextCh();
                if (lex->currCh>='0' && lex->currCh<='7') {
                  buf[n++] = lex->currCh; jslGetNextCh();
                  if (lex->currCh>='0' && lex->currCh<='7') {
                    buf[n++] = lex->currCh; jslGetNextCh();
                  }
                }
                buf[n]=0;
                ch = (char)stringToInt(buf);
              } else {
                // for anything else, just push the character through
                jslGetNextCh();
              }
              break;
            }
            jslTokenAppendChar(ch);
            jsvStringIteratorAppend(&it, ch);
          } else {
            jslTokenAppendChar(lex->currCh);
            jsvStringIteratorAppend(&it, lex->currCh);
            jslGetNextCh();
          }
        }
        jsvStringIteratorFree(&it);
        lex->tk = lex->currCh==delim ? LEX_STR : LEX_UNFINISHED_STR;
        jslGetNextCh();
      } break;
      case JSLJT_EXCLAMATION: jslSingleChar();
      if (lex->currCh=='=') { // !=
        lex->tk = LEX_NEQUAL;
        jslGetNextCh();
        if (lex->currCh=='=') { // !==
          lex->tk = LEX_NTYPEEQUAL;
          jslGetNextCh();
        }
      } break;
      case JSLJT_PLUS: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_PLUSEQUAL;
        jslGetNextCh();
      } else if (lex->currCh=='+') {
        lex->tk = LEX_PLUSPLUS;
        jslGetNextCh();
      } break;
      case JSLJT_MINUS: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_MINUSEQUAL;
        jslGetNextCh();
      } else if (lex->currCh=='-') {
        lex->tk = LEX_MINUSMINUS;
        jslGetNextCh();
      } break;
      case JSLJT_AND: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_ANDEQUAL;
        jslGetNextCh();
      } else if (lex->currCh=='&') {
        lex->tk = LEX_ANDAND;
        jslGetNextCh();
      } break;
      case JSLJT_OR: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_OREQUAL;
        jslGetNextCh();
      } else if (lex->currCh=='|') {
        lex->tk = LEX_OROR;
        jslGetNextCh();
      } break;
      case JSLJT_TOPHAT: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_XOREQUAL;
        jslGetNextCh();
      } break;
      case JSLJT_STAR: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_MULEQUAL;
        jslGetNextCh();
      } break;
      case JSLJT_FORWARDSLASH: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_DIVEQUAL;
        jslGetNextCh();
      } break;
      case JSLJT_PERCENT: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_MODEQUAL;
        jslGetNextCh();
      } break;
      case JSLJT_EQUAL: jslSingleChar();
      if (lex->currCh=='=') { // ==
        lex->tk = LEX_EQUAL;
        jslGetNextCh();
        if (lex->currCh=='=') { // ===
          lex->tk = LEX_TYPEEQUAL;
          jslGetNextCh();
        }
      } break;
      case JSLJT_LESSTHAN: jslSingleChar();
      if (lex->currCh=='=') { // <=
        lex->tk = LEX_LEQUAL;
        jslGetNextCh();
      } else if (lex->currCh=='<') { // <<
        lex->tk = LEX_LSHIFT;
        jslGetNextCh();
        if (lex->currCh=='=') { // <<=
          lex->tk = LEX_LSHIFTEQUAL;
          jslGetNextCh();
        }
      } break;
      case JSLJT_GREATERTHAN: jslSingleChar();
      if (lex->currCh=='=') { // >=
        lex->tk = LEX_GEQUAL;
        jslGetNextCh();
      } else if (lex->currCh=='>') { // >>
        lex->tk = LEX_RSHIFT;
        jslGetNextCh();
        if (lex->currCh=='=') { // >>=
          lex->tk = LEX_RSHIFTEQUAL;
          jslGetNextCh();
        } else if (lex->currCh=='>') { // >>>
          jslGetNextCh();
          if (lex->currCh=='=') { // >>>=
            lex->tk = LEX_RSHIFTUNSIGNEDEQUAL;
            jslGetNextCh();
          } else {
            lex->tk = LEX_RSHIFTUNSIGNED;
          }
        }
      } break;

      case JSLJT_SINGLECHAR: jslSingleChar(); break;
      default: assert(0);break;
    }
    }
  }
}
示例#8
0
文件: jslex.c 项目: CWBudde/Espruino
void jslGetNextToken() {
  jslGetNextToken_start:
  // Skip whitespace
  while (isWhitespace(lex->currCh))
    jslGetNextCh();
  // Search for comments
  if (lex->currCh=='/') {
    // newline comments
    if (jslNextCh()=='/') {
      while (lex->currCh && lex->currCh!='\n') jslGetNextCh();
      jslGetNextCh();
      goto jslGetNextToken_start;
    }
    // block comments
    if (jslNextCh()=='*') {
      jslGetNextCh();
      jslGetNextCh();
      while (lex->currCh && !(lex->currCh=='*' && jslNextCh()=='/'))
        jslGetNextCh();
      if (!lex->currCh) {
        lex->tk = LEX_UNFINISHED_COMMENT;
        return; /* an unfinished multi-line comment. When in interactive console,
                   detect this and make sure we accept new lines */
      }
      jslGetNextCh();
      jslGetNextCh();
      goto jslGetNextToken_start;
    }
  }
  int lastToken = lex->tk;
  lex->tk = LEX_EOF;
  lex->tokenl = 0; // clear token string
  if (lex->tokenValue) {
    jsvUnLock(lex->tokenValue);
    lex->tokenValue = 0;
  }
  // record beginning of this token
  lex->tokenLastStart = jsvStringIteratorGetIndex(&lex->tokenStart.it) - 1;
  /* we don't lock here, because we know that the string itself will be locked
   * because of lex->sourceVar */
  lex->tokenStart.it = lex->it;
  lex->tokenStart.currCh = lex->currCh;
  // tokens
  if (((unsigned char)lex->currCh) < jslJumpTableStart ||
      ((unsigned char)lex->currCh) > jslJumpTableEnd) {
    // if unhandled by the jump table, just pass it through as a single character
    jslSingleChar();
  } else {
    switch(jslJumpTable[((unsigned char)lex->currCh) - jslJumpTableStart]) {
    case JSLJT_ID: {
      while (isAlpha(lex->currCh) || isNumeric(lex->currCh) || lex->currCh=='$') {
        jslTokenAppendChar(lex->currCh);
        jslGetNextCh();
      }
      lex->tk = LEX_ID;
      // We do fancy stuff here to reduce number of compares (hopefully GCC creates a jump table)
      switch (lex->token[0]) {
      case 'b': if (jslIsToken("break", 1)) lex->tk = LEX_R_BREAK;
      break;
      case 'c': if (jslIsToken("case", 1)) lex->tk = LEX_R_CASE;
      else if (jslIsToken("catch", 1)) lex->tk = LEX_R_CATCH;
      else if (jslIsToken("class", 1)) lex->tk = LEX_R_CLASS;
      else if (jslIsToken("const", 1)) lex->tk = LEX_R_CONST;
      else if (jslIsToken("continue", 1)) lex->tk = LEX_R_CONTINUE;
      break;
      case 'd': if (jslIsToken("default", 1)) lex->tk = LEX_R_DEFAULT;
      else if (jslIsToken("delete", 1)) lex->tk = LEX_R_DELETE;
      else if (jslIsToken("do", 1)) lex->tk = LEX_R_DO;
      else if (jslIsToken("debugger", 1)) lex->tk = LEX_R_DEBUGGER;
      break;
      case 'e': if (jslIsToken("else", 1)) lex->tk = LEX_R_ELSE;
      else if (jslIsToken("extends", 1)) lex->tk = LEX_R_EXTENDS;
      break;
      case 'f': if (jslIsToken("false", 1)) lex->tk = LEX_R_FALSE;
      else if (jslIsToken("finally", 1)) lex->tk = LEX_R_FINALLY;
      else if (jslIsToken("for", 1)) lex->tk = LEX_R_FOR;
      else if (jslIsToken("function", 1)) lex->tk = LEX_R_FUNCTION;
      break;
      case 'i': if (jslIsToken("if", 1)) lex->tk = LEX_R_IF;
      else if (jslIsToken("in", 1)) lex->tk = LEX_R_IN;
      else if (jslIsToken("instanceof", 1)) lex->tk = LEX_R_INSTANCEOF;
      break;
      case 'l': if (jslIsToken("let", 1)) lex->tk = LEX_R_LET;
      break;
      case 'n': if (jslIsToken("new", 1)) lex->tk = LEX_R_NEW;
      else if (jslIsToken("null", 1)) lex->tk = LEX_R_NULL;
      break;
      case 'o': if (jslIsToken("of", 1)) lex->tk = LEX_R_OF;
      break;
      case 'r': if (jslIsToken("return", 1)) lex->tk = LEX_R_RETURN;
      break;
      case 's': if (jslIsToken("static", 1)) lex->tk = LEX_R_STATIC;
      else if (jslIsToken("super", 1)) lex->tk = LEX_R_SUPER;
      else if (jslIsToken("switch", 1)) lex->tk = LEX_R_SWITCH;
      break;
      case 't': if (jslIsToken("this", 1)) lex->tk = LEX_R_THIS;
      else if (jslIsToken("throw", 1)) lex->tk = LEX_R_THROW;
      else if (jslIsToken("true", 1)) lex->tk = LEX_R_TRUE;
      else if (jslIsToken("try", 1)) lex->tk = LEX_R_TRY;
      else if (jslIsToken("typeof", 1)) lex->tk = LEX_R_TYPEOF;
      break;
      case 'u': if (jslIsToken("undefined", 1)) lex->tk = LEX_R_UNDEFINED;
      break;
      case 'w': if (jslIsToken("while", 1)) lex->tk = LEX_R_WHILE;
      break;
      case 'v': if (jslIsToken("var", 1)) lex->tk = LEX_R_VAR;
      else if (jslIsToken("void", 1)) lex->tk = LEX_R_VOID;
      break;
      default: break;
      } break;
      case JSLJT_NUMBER: {
        // TODO: check numbers aren't the wrong format
        bool canBeFloating = true;
        if (lex->currCh=='.') {
          jslGetNextCh();
          if (isNumeric(lex->currCh)) {
            // it is a float
            lex->tk = LEX_FLOAT;
            jslTokenAppendChar('.');
          } else {
            // it wasn't a number after all
            lex->tk = '.';
            break;
          }
        } else {
          if (lex->currCh=='0') {
            jslTokenAppendChar(lex->currCh);
            jslGetNextCh();
            if ((lex->currCh=='x' || lex->currCh=='X') ||
                (lex->currCh=='b' || lex->currCh=='B') ||
                (lex->currCh=='o' || lex->currCh=='O')) {
              canBeFloating = false;
              jslTokenAppendChar(lex->currCh); jslGetNextCh();
            }
          }
          lex->tk = LEX_INT;
          while (isNumeric(lex->currCh) || (!canBeFloating && isHexadecimal(lex->currCh))) {
            jslTokenAppendChar(lex->currCh);
            jslGetNextCh();
          }
          if (canBeFloating && lex->currCh=='.') {
            lex->tk = LEX_FLOAT;
            jslTokenAppendChar('.');
            jslGetNextCh();
          }
        }
        // parse fractional part
        if (lex->tk == LEX_FLOAT) {
          while (isNumeric(lex->currCh)) {
            jslTokenAppendChar(lex->currCh);
            jslGetNextCh();
          }
        }
        // do fancy e-style floating point
        if (canBeFloating && (lex->currCh=='e'||lex->currCh=='E')) {
          lex->tk = LEX_FLOAT;
          jslTokenAppendChar(lex->currCh); jslGetNextCh();
          if (lex->currCh=='-' || lex->currCh=='+') { jslTokenAppendChar(lex->currCh); jslGetNextCh(); }
          while (isNumeric(lex->currCh)) {
            jslTokenAppendChar(lex->currCh); jslGetNextCh();
          }
        }
      } break;
      case JSLJT_STRING: jslLexString(); break;
      case JSLJT_EXCLAMATION: jslSingleChar();
      if (lex->currCh=='=') { // !=
        lex->tk = LEX_NEQUAL;
        jslGetNextCh();
        if (lex->currCh=='=') { // !==
          lex->tk = LEX_NTYPEEQUAL;
          jslGetNextCh();
        }
      } break;
      case JSLJT_PLUS: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_PLUSEQUAL;
        jslGetNextCh();
      } else if (lex->currCh=='+') {
        lex->tk = LEX_PLUSPLUS;
        jslGetNextCh();
      } break;
      case JSLJT_MINUS: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_MINUSEQUAL;
        jslGetNextCh();
      } else if (lex->currCh=='-') {
        lex->tk = LEX_MINUSMINUS;
        jslGetNextCh();
      } break;
      case JSLJT_AND: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_ANDEQUAL;
        jslGetNextCh();
      } else if (lex->currCh=='&') {
        lex->tk = LEX_ANDAND;
        jslGetNextCh();
      } break;
      case JSLJT_OR: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_OREQUAL;
        jslGetNextCh();
      } else if (lex->currCh=='|') {
        lex->tk = LEX_OROR;
        jslGetNextCh();
      } break;
      case JSLJT_TOPHAT: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_XOREQUAL;
        jslGetNextCh();
      } break;
      case JSLJT_STAR: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_MULEQUAL;
        jslGetNextCh();
      } break;
      case JSLJT_FORWARDSLASH:
      // yay! JS is so awesome.
      if (lastToken==LEX_EOF ||
          lastToken=='!' ||
          lastToken=='%' ||
          lastToken=='&' ||
          lastToken=='*' ||
          lastToken=='+' ||
          lastToken=='-' ||
          lastToken=='/' ||
          lastToken=='<' ||
          lastToken=='=' ||
          lastToken=='>' ||
          lastToken=='?' ||
          (lastToken>=_LEX_OPERATOR_START && lastToken<=_LEX_OPERATOR_END) ||
          (lastToken>=_LEX_R_LIST_START && lastToken<=_LEX_R_LIST_END) || // keywords
          lastToken==LEX_R_CASE ||
          lastToken==LEX_R_NEW ||
          lastToken=='[' ||
          lastToken=='{' ||
          lastToken=='}' ||
          lastToken=='(' ||
          lastToken==',' ||
          lastToken==';' ||
          lastToken==':' ||
          lastToken==LEX_ARROW_FUNCTION) {
        // EOF operator keyword case new [ { } ( , ; : =>
        // phew. We're a regex
        jslLexRegex();
      } else {
        jslSingleChar();
        if (lex->currCh=='=') {
          lex->tk = LEX_DIVEQUAL;
          jslGetNextCh();
        }
      } break;
      case JSLJT_PERCENT: jslSingleChar();
      if (lex->currCh=='=') {
        lex->tk = LEX_MODEQUAL;
        jslGetNextCh();
      } break;
      case JSLJT_EQUAL: jslSingleChar();
      if (lex->currCh=='=') { // ==
        lex->tk = LEX_EQUAL;
        jslGetNextCh();
        if (lex->currCh=='=') { // ===
          lex->tk = LEX_TYPEEQUAL;
          jslGetNextCh();
        }
      } else if (lex->currCh=='>') { // =>
        lex->tk = LEX_ARROW_FUNCTION;
        jslGetNextCh();
      } break;
      case JSLJT_LESSTHAN: jslSingleChar();
      if (lex->currCh=='=') { // <=
        lex->tk = LEX_LEQUAL;
        jslGetNextCh();
      } else if (lex->currCh=='<') { // <<
        lex->tk = LEX_LSHIFT;
        jslGetNextCh();
        if (lex->currCh=='=') { // <<=
          lex->tk = LEX_LSHIFTEQUAL;
          jslGetNextCh();
        }
      } break;
      case JSLJT_GREATERTHAN: jslSingleChar();
      if (lex->currCh=='=') { // >=
        lex->tk = LEX_GEQUAL;
        jslGetNextCh();
      } else if (lex->currCh=='>') { // >>
        lex->tk = LEX_RSHIFT;
        jslGetNextCh();
        if (lex->currCh=='=') { // >>=
          lex->tk = LEX_RSHIFTEQUAL;
          jslGetNextCh();
        } else if (lex->currCh=='>') { // >>>
          jslGetNextCh();
          if (lex->currCh=='=') { // >>>=
            lex->tk = LEX_RSHIFTUNSIGNEDEQUAL;
            jslGetNextCh();
          } else {
            lex->tk = LEX_RSHIFTUNSIGNED;
          }
        }
      } break;

      case JSLJT_SINGLECHAR: jslSingleChar(); break;
      default: assert(0);break;
    }
    }
  }
}