Example #1
0
 public function __construct($identifier, $value, $type, $_symTable)
 {
     $this->identifier = $identifier;
     switch ($type) {
         case Token::UNSIGNED_INTEGER:
         case Token::SIGNED_INTEGER:
             $this->type = Globals::getSimpleType('integer');
             $this->value = $value + 0;
             break;
         case Token::UNSIGNED_REAL:
         case Token::UNSIGNED_REAL_E:
         case Token::SIGNED_REAL:
         case Token::SIGNED_REAL_E:
             $this->type = Globals::getSimpleType('real');
             $this->value = $value + 0.0;
             break;
         case Token::CHARACTER_STRING:
             $this->type = Globals::getSimpleType('string');
             $this->value = $value;
             break;
         case Token::BOOLEAN_CONST:
             $this->type = Globals::getSimpleType('boolean');
             $this->value = $value == 'true';
             break;
         default:
             throw new \Exception('UNKNOWN TOKEN TYPE');
     }
     if ($this->type == null) {
         throw new \Exception('BUILT-IN TYPES ARE NOT INITIALIZED');
     }
 }
Example #2
0
 public function __construct($scanner, $_symTable)
 {
     parent::requireKeyword($scanner, 'for');
     $scanner->next();
     $this->counter = VariableAccess::parse($scanner, $_symTable);
     if (!SymSimpleType::equal($this->counter->symType, Globals::getSimpleType('integer'))) {
         SemanticException::raw($scanner, 'Counter must be integer');
     }
     parent::requireOperator($scanner, ':=');
     $scanner->next();
     $this->from = new Expression($scanner, $_symTable);
     $this->from = TypeCast::tryTypeCast($this->from, 'integer');
     switch ($scanner->get()->getValue()) {
         case 'to':
             $this->direction = self::DIRECTION_ASC;
             break;
         case 'downto':
             $this->direction = self::DIRECTION_DESC;
             break;
         default:
             parent::simpleException($scanner, ['<KEYWORD \'to\'>', '<KEYWORD \'downto\'>']);
     }
     $scanner->next();
     $this->to = new Expression($scanner, $_symTable);
     $this->to = TypeCast::tryTypeCast($this->to, 'integer');
     parent::requireKeyword($scanner, 'do');
     $scanner->next();
     Globals::$loopDepth++;
     $this->statements = CompoundStatement::smartParse($scanner, $_symTable);
     Globals::$loopDepth--;
 }
Example #3
0
 public function __construct($operand, $operator, $_symTable)
 {
     if (!SymSimpleType::equal($operand->symType, Globals::getSimpleType('integer')) && !SymSimpleType::equal($operand->symType, Globals::getSimpleType('real'))) {
         SemanticException::invalidTypeCast($operand->symType, Globals::getSimpleType('real'));
     }
     $this->symType = $operand->symType;
     $this->operand = $operand;
     $this->operator = $operator;
 }
Example #4
0
 public static function tryTypeCast($obj, $toType, $simpleType = true)
 {
     $comparer = $simpleType ? "vendor\\SemanticParser\\Nodes\\SymSimpleType" : get_class($obj->symType);
     if ($simpleType) {
         $toType = Globals::getSimpleType($toType);
     }
     if (!$comparer::equal($obj->symType, $toType)) {
         return new TypeCast($obj, $toType);
     }
     return $obj;
 }
Example #5
0
 public function __construct($scanner)
 {
     $scanner->next();
     $this->symTable = new SymTable(null);
     Globals::init($this->symTable);
     $this->heading = new ProgramHeading($scanner);
     parent::semicolonPass($scanner);
     $this->block = new Block($scanner, $this->symTable);
     parent::requireOperator($scanner, '.');
     $scanner->next();
     if (!$scanner->get()->isEOF()) {
         parent::simpleException($scanner, ['<EOF>']);
     }
 }
Example #6
0
 public function __construct($left, $right, $operator, $_symTable)
 {
     switch ($operator->getValue()) {
         case 'div':
         case 'mod':
             $right = TypeCast::tryTypeCast($right, 'integer');
             $left = TypeCast::tryTypeCast($left, 'integer');
             $this->symType = Globals::getSimpleType('integer');
             break;
         case 'and':
         case 'or':
         case 'xor':
             $right = TypeCast::tryTypeCast($right, 'boolean');
             $left = TypeCast::tryTypeCast($left, 'boolean');
             $this->symType = Globals::getSimpleType('boolean');
             break;
         case '*':
         case '+':
         case '-':
             if (SymSimpleType::equal($left->symType, Globals::getSimpleType('integer')) && SymSimpleType::equal($right->symType, Globals::getSimpleType('integer'))) {
                 $this->symType = Globals::getSimpleType('integer');
                 break;
             }
         case '/':
             $left = TypeCast::tryTypeCast($left, 'real');
             $right = TypeCast::tryTypeCast($right, 'real');
             $this->symType = Globals::getSimpleType('real');
             break;
         case '>':
         case '<':
         case '>=':
         case '<=':
         case '<>':
         case '=':
             if (SymSimpleType::equal($left->symType, Globals::getSimpleType('integer')) && SymSimpleType::equal($right->symType, Globals::getSimpleType('integer'))) {
                 $this->symType = Globals::getSimpleType('boolean');
                 break;
             }
             $left = TypeCast::tryTypeCast($left, 'real');
             $right = TypeCast::tryTypeCast($right, 'real');
             $this->symType = Globals::getSimpleType('boolean');
             break;
         default:
             throw new \Exception('OPERATOR NOT IMPLEMENTED YET');
     }
     $this->left = $left;
     $this->right = $right;
     $this->operator = $operator;
 }
Example #7
0
 public static function recognizeSimpleType($const, $_symTable)
 {
     switch ($const->type) {
         case Token::UNSIGNED_NUMBER:
         case Token::UNSIGNED_INTEGER:
         case Token::SIGNED_NUMBER:
         case Token::SIGNED_INTEGER:
             return Globals::getSimpleType('integer');
         case Token::UNSIGNED_REAL:
         case Token::UNSIGNED_REAL_E:
         case Token::SIGNED_REAL:
         case Token::SIGNED_REAL_E:
             return Globals::getSimpleType('real');
         case Token::CHARACTER_STRING:
             return Globals::getSimpleType('string');
         case Token::BOOLEAN_CONST:
             return Globals::getSimpleType('boolean');
         default:
             throw new \Exception('CONST TYPE NOT IMPLEMENTED YET!');
     }
 }