Login   Register  
PHP Classes
elePHPant
Icontem

File: src/eMacros/Symbol.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Emmanuel Antico  >  eMacros  >  src/eMacros/Symbol.php  >  Download  
File: src/eMacros/Symbol.php
Role: Class source
Content type: text/plain
Description: Class source
Class: eMacros
PHP LISP language interpreter
Author: By
Last change:
Date: 2014-01-13 05:17
Size: 1,199 bytes
 

Contents

Class file image Download
<?php
namespace eMacros;

class 
Symbol implements Expression {
    
/**
     * Symbol expression
     * @var string
     */
    
public $symbol;
    
    
/**
     * Symbol package
     * @var NULL|string
     */
    
public $package;
    
    
/**
     * Package prefix regex
     * @var string
     */
    
const PACKAGE_PREFIX '/^([a-z|A-Z|_][\w]+)::/';
    
    
/**
     * Symbol validation regex
     * @var string
     */
    
const PATTERN '{^[^\s\d(){}\[\]"\';][^\s\'"(){}\[\];]*$}';

    public static function 
validateSymbol($symbol) {
        if (!
is_string($symbol)) {
            throw new \
UnexpectedValueException(sprintf("Symbol: unexpected value of type '%s'."gettype($symbol)));
        }
        
        if (!
preg_match(self::PATTERN$symbol)) {
            throw new \
UnexpectedValueException(sprintf("Symbol: '%s' is not a valid symbol."$symbol));
        }
    
        return 
$symbol;
    }
    
    public function 
__construct($symbol) {
        
//obtain package, if any
        
if (preg_match(self::PACKAGE_PREFIX$symbol$matches)) {
            
$this->package $matches[1];
            
$symbol substr($symbolstrlen($this->package) + 2);
        }

        
$this->symbol self::validateSymbol($symbol);
    }
    
    public function 
evaluate(Scope $scope) {
        return 
$scope[$this];
    }
    
    public function 
__toString() {
        return 
$this->symbol;
    }
}
?>