Location: PHPKode > scripts > DFL > dfl/dfl.class.php
<?php

/*

___________.__           ________          _____                        .___
\__    ___/|  |__   ____ \______ \   _____/ ____\____    ____  ____   __| _/
  |    |   |  |  \_/ __ \ |    |  \_/ __ \   __\\__  \ _/ ___\/ __ \ / __ | 
  |    |   |   Y  \  ___/ |    `   \  ___/|  |   / __ \\  \__\  ___// /_/ | 
  |____|   |___|  /\___  >_______  /\___  >__|  (____  /\___  >___  >____ | 
                \/     \/        \/     \/           \/     \/    \/     \/ 

Introducing DFL. Open Source.

[Author] -> TheDefaced Boredom Devision

The standard DFL language setup (the one in this file unedited) has a wiki documentation site.
	* http://thedefaced.org/dfl/

*/

error_reporting(0);
function newline()
	{
		return "\n";
	};
function getmicrotime()
	{ 
	    $temparray = split(" ",microtime()); 
	    $returntime = $temparray[0] + $temparray[1]; 
	    return $returntime; 
	};
function parseline($data)
	{
		$content = $GLOBALS["DFL"]->safe_parse_line($data);
		$GLOBALS["DFL"]->DFL_INTERNALS["LEVEL"]--;
		return $content;
	};
$mainfile = $argv[0];
##################
define ("ERROR_NOTICE", 1);
define ("ERROR_WARNING", 2);
define ("ERROR_FATAL", 3);
define ("ERROR_EXCEPTION", 4);
Class DFL
	{
		var $vars = array();
		var $funcs = array();
		var $log = array();
		var $methods = array();
		var $debug;
		var $buffer = array();
		var $depth = 0;
		var $error_reporting = 1;
		var $inc_num = 0;
		var $DFL_INTERNALS = array();
		var $safe_parse_buffer = array();
		var $return_content = "";
		var $returned_last = FALSE;

		function init($file, $debug = FALSE, $build = FALSE, $safe = FALSE)
			{
				$this->DFL_INTERNALS = array("FILE" => $file, "LNUM" => 0, "LEVEL" => 0);
				$this->datalogging("DFL", "Engine to process $file");
				$this->debug = $debug;
				if (($debug) && ($build))
					{
						echo "############ DEBUG CONSOLE ############\n\t[Stack Logging]\n";
					};
				if (!is_readable($file))
					{
						$this->error(ERROR_FATAL, 0, "DFL Engine", "Could not read from $file");
						return FALSE;
					};
				$data = implode("", file($file));
				if (($debug) && ($build))
					{
						$TIMER_START = getmicrotime();
					};
				$this->parsedata($data, $file, $safe);
				if (($debug) && ($build))
					{
						$TIMER_END = getmicrotime();
						$time_taken = $TIMER_END - $TIMER_START;
					};
				if (($debug) && ($build))
					{
						echo "\t[Engine Logging]\n";
						foreach ($this->log as $log)
							{
								echo $log . "\n";
							};
						echo "\t[Output Buffer]\n";
						foreach ($this->buffer as $buffer)
							{
								echo $buffer;
							};
						echo "\n\t[Function Dump]\n";
						foreach ($this->funcs as $name => $data)
							{
								echo $name . "\n";
							};
						echo "\n\t[Variable Dump]\n";
						foreach ($this->vars as $name => $data)
							{
								echo $name . "\n";
							};
						echo "\n\t[Public Method Dump]\n";
						foreach ($this->methods as $methodname => $data)
							{
								echo $methodname . "\t(" . count($data) . " lines)\n";
							}
						echo "\n\t[Other]\n";
						echo "Execution time: " . $time_taken . " second(s)\n";
						$this->inc_num = 1 + $this->inc_num;
						echo "Processed: " . $this->inc_num . " file(s)\n";
						echo "Variable Count: " . count($this->vars) . "\n";
						echo "Function Count: " . count($this->funcs) . "\n";
						$this->buffer = implode("", $this->buffer);
						$buffer_size = strlen($this->buffer);
						echo "Buffer Length: $buffer_size\n";
					};
				return TRUE;
			}

		function parsedata($data, $file, $safe = FALSE)
			{
				$data = explode("\n", $data);
				foreach ($data as $lnum => $line)
					{
						$lnum++;
						$line = trim($line);
						if ($this->verifyline($line))
							{
								$this->parseline($line, 0, $lnum, $file, $safe);
								if ($this->debug)
									{
										if (!$safe)
											{
												if (!empty($this->safe_parse_buffer))
													{
														foreach ($this->safe_parse_buffer as $buffer)
															{
																echo $buffer;
															};
														$this->safe_parse_buffer = array();
													};
											};
									};
							};
					};
			}
		
		function datalogging($module, $data)
			{
				$levelmk = $this->depth;
				$tabs = "";
				while ($levelmk > 1)
					{
						$tabs .= "     ";
						$levelmk--;
					};
				if ($level != 0)
					{
						$tabs .= "|--->";
					};
				$this->log[] = $tabs . "[DFL]::$module -> $data";
			}
		
		function error($level, $line, $file, $data)
			{
				if ($level >= $this->error_reporting)
					{
						switch ($level)
							{
								case 1:
									// ERROR_NOTICE
									echo "\n[DFL]::error -> Notice: $data on line $line in $file\n";
								break;
								case 2:
									// ERROR_WARNING
									echo "\n[DFL]::error -> Warning: $data on line $line in $file\n";
								break;
								case 3:
									// ERROR_FATAL
									echo "\n[DFL]::error -> Fatal Error: $data on line $line in $file\n";
									exit;
								break;
								case 4:
									// ERROR_EXCEPTION
									echo "\n[DFL]::error -> Fatal Exception: $data on line $line in $file\n";
									exit;
								break;
							}
					};
			}
		
		function verifyline($data)
			{
				if (empty($data))
					{
						return FALSE;
					}
				elseif (substr($data, 0, 1) == "#")
					{
						return FALSE;
					}
				elseif (substr($data, 0, 2) == "//")
					{
						return FALSE;
					};
				return TRUE;
			}
		
		function safe_parse_line($line)
			{
				return $this->parseline($line, $this->DFL_INTERNALS["LEVEL"], $this->DFL_INTERNALS["LNUM"], $this->DFL_INTERNALS["FILE"], TRUE);
			}
		
		function parseline($line = "", $level = 0, $lnum = 0, $file = "", $safe = FALSE)
			{
				$this->DFL_INTERNALS["LEVEL"] = $level;
				$this->DFL_INTERNALS["LNUM"] = $lnum;
				$this->DFL_INTERNALS["FILE"] = $file;
				if (empty($line))
					{
						$this->error(ERROR_NOTICE, $lnum, $file, "Could not process data as a statement");
						return FALSE;
					};
				if ($this->debug && !$safe)
					{
						echo "DFL Debug:($level):";
						$levelmk = $level;
						while ($levelmk > 1)
							{
								echo "     ";
								$levelmk--;
							};
						if ($level != 0)
							{
								echo "|--->";
							};
						echo $line . "\n";
					};
				if ($safe && $this->debug)
					{
						$debug_info = "";
						$debug_info .= "DFL Debug:($level):";
						$levelmk = $level;
						while ($levelmk > 1)
							{
								$debug_info .= "     ";
								$levelmk--;
							};
						if ($level != 0)
							{
								$debug_info .= "|--->";
							};
						$debug_info .= $line . "\n";
						$this->safe_parse_buffer[] = $debug_info;
					};
				$line = explode(" ", $line);
				switch(strtoupper($line[0]))
					{
						case "VAR":
							if (preg_match("/^[a-zA-Z_]{1,16}$/", $line[1]))
								{
									if (empty($line[2]))
										{
											return $this->vars[$line[1]];
										}
									else
										{
											$varname = $line[1];
											$line[0] = "";
											$line[1] = "";
											$line = implode(" ", $line);
											$line = trim($line);
											$this->vars[$varname] = $this->parseline($line, $level + 1, $lnum, $file, $safe);
											return $this->vars[$varname];
										};
								}
							else
								{
									if (empty($line[1]))
										{
											$this->error(ERROR_WARNING, $lnum, $file, "The variable on stack level $level couldn't be created because there was no name supplied as an argument");
											return FALSE;
										};
									$this->error(ERROR_WARNING, $lnum, $file, "The variable $line[1] couldn't be created because the name has invalid syntax");
								};
						break;
						case "FNC":
							if (preg_match("/^[a-zA-Z_]{1,20}$/", $line[1]))
								{
									$line[0] = "";
									$name = $line[1];
									$line[1] = "";
									$line = implode(" ", $line);
									$line = trim($line);
									if (empty($line))
										{
											$this->error(ERROR_WARNING, $lnum, $file, "The function $name could not be created because no second argument exists that must be present");
											return FALSE;
										}
									if (!empty($this->funcs[$name]))
										{
											$this->error(ERROR_NOTICE, $lnum, $file, "The function $name has already been created, overwriting");
										};
									$line = $this->parseline($line, $level + 1, $lnum, $file, $safe);
									if ($this->debug)
										{
											$this->funcs[$name] = create_function('$args = NULL', str_replace("__FILE__", "", "ob_start(); " . $line . ' $GLOBALS["DFL"]->buffer[] = ob_get_clean();')) OR $this->error(ERROR_WARNING, $lnum, $file, "FNC was unable to create the function");
										}
									else
										{
											$this->funcs[$name] = create_function('$args = NULL', str_replace("__FILE__", "", $line)) OR $this->error(ERROR_WARNING, $lnum, $file, "FNC was unable to create the function");
										};
								}
							else
								{
									if (empty($line[1]))
										{
											$this->error(ERROR_WARNING, $lnum, $file, "The function on stack level $level couldn't be created because there was no name supplied as an argument");
											return FALSE;
										};
									$this->error(ERROR_WARNING, $lnum, $file, "The function $line[1] couldn't be created because the name has invalid syntax");
								};
						break;
						case "RUN":
							if (preg_match("/^[a-zA-Z_]{1,20}$/", $line[1]))
								{
									$line[0] = "";
									$name = $line[1];
									$line[1] = "";
									$line = implode(" ", $line);
									$line = trim($line);
									if (empty($this->funcs[$name]))
										{
											$this->error(ERROR_WARNING, $lnum, $file, "Could not execute $name as a function because it does not exist");
											return FALSE;
										}
									if (empty($line))
										{
											return $this->funcs[$name]();
										}
									else
										{
											$args = $this->parseline($line, $level + 1, $lnum, $file, $safe);
											$return = $this->funcs[$name]($args);
											return $return;
										};
								}
							else
								{
									$this->error(ERROR_WARNING, $lnum, $file, "Could not execute the function because no valid name was given");
									return FALSE;
								};
						break;
						case "STR":
							$line[0] = "";
							$line = implode(" ", $line);
							$line = trim($line);
							return $line;
						break;
						case "INC":
							$line[0] = "";
							$line = implode(" ", $line);
							$line = trim($line);
							$line = $this->parseline($line, $level + 1, $lnum, $file, $safe);
							if (!is_readable($line))
								{
									$this->error(ERROR_WARNING, $lnum, $file, "Unable to include $line into execution; file could not be read");
								}
							else
								{
									$this->depth++;
									if (!$this->init($line, $this->debug, NULL, $safe))
										{
											$this->error(ERROR_WARNING, $lnum, $file, "DFL engine processing error, unable to process $line");
										};
									$this->depth--;
									$this->inc_num++;
								};
						break;
						case "ERR":
							$line[0] = "";
							$line = implode(" ", $line);
							$line = trim($line);
							$err = $line;
							$line = array();
							$line[1] = $err;
							if (empty($line[1]))
								{
									return $this->error_reporting;
								}
							else
								{
									$line[1] = $this->parseline($line[1], $level + 1, $lnum, $file, $safe);
									$accept = range(1, 4);
									if (in_array(intval($line[1]), $accept))
										{
											$this->error_reporting = $line[1];
										}
									else
										{
											$accept = array();
											$accept[] = "ERROR_NOTICE";
											$accept[] = "ERROR_WARNING";
											$accept[] = "ERROR_FATAL";
											$accept[] = "ERROR_EXCEPTION";
											if (in_array($line[1], $accept))
												{
													$this->error_reporting = constant($line[1]);
												}
											else
												{
													$this->error(ERROR_WARNING, $lnum, $file, $line[1] . " is not an acceptable error_reporting level integer");
												};
										};
									return $this->error_reporting;
								};
						break;
						case "DEL":
							if (empty($line[1]))
								{
									$this->error(ERROR_NOTICE, $lnum, $file, "No argument provided to DEL");
									return FALSE;
								}
							else
								{
									if (empty($this->funcs[$line[1]]))
										{
											$this->error(ERROR_NOTICE, $lnum, $file, "The provided function ($line[1]) does not exist");
										}
									else
										{
											$this->funcs[$line[1]] = "";
											unset($this->funcs[$line[1]]);
										}
								};
						break;
						case "ARR":
							$line[0] = "";
							$line = implode(" ", $line);
							$line = trim($line);
							$line = explode(",", $line);
							if (!empty($line))
								{
									foreach ($line as $exp)
										{
											$returns[] = $this->parseline($exp, $level + 1, $lnum, $file, $safe);
										};
									return $returns;
								}
							else
								{
									return array();
								};
						break;
						case "DEP":
							return $this->depth;
						break;
						case "EVL":
							$line[0] = "";
							$line = implode(" ", $line);
							$line = trim($line);
							$line = $this->parseline($line, $level + 1, $lnum, $file, $safe);
							return $this->parseline($line, $level + 1, $lnum, $file, $safe);
						break;
						case "RET":
							$line[0] = "";
							$line = implode(" ", $line);
							$line = trim($line);
							$this->return_content = $this->parseline($line, $level + 1, $lnum, $file, $safe);
							$this->returned_last = TRUE;
						break;
						default:
							if (!$this->parsemethod($line))
								{
									$this->DFL_INTERNALS["LEVEL"]--;
									if (empty($line[0]))
										{
											$this->error(ERROR_WARNING, $lnum, $file, "Errorous spatial occurrence providing register absence found, ignoring line");
										}
									else
										{
											$this->error(ERROR_WARNING, $lnum, $file, "Could not process $line[0] as a register");
										};
								}
							else
								{
									$this->DFL_INTERNALS["LEVEL"]--;
									$method = $this->return_content;
									$this->return_content = TRUE;
									return $method;
								}
						break;
					};
			}
		
		function parsemethod($data)
			{
				$this->DFL_INTERNALS["LEVEL"]++;
				if (substr($data[0], 0, 6) == "Method")
					{
						switch ($data[0])
							{
								case "MethodBuild":
									$name = $data[1];
									if ((substr($name, 0, 6) != "Method") && preg_match("/^[a-zA-Z_]{4,20}$/", $name))
										{
											if (!isset($this->methods[$name]))
												{
													$this->methods[$name] = array();
												}
											else
												{
													$this->error(ERROR_WARNING, $this->DFL_INTERNALS['LNUM'], $this->DFL_INTERNALS['FILE'], "Invalid name supplied for building method");
												};
										}
									else
										{
											$this->error(ERROR_WARNING, $this->DFL_INTERNALS['LNUM'], $this->DFL_INTERNALS['FILE'], "Invalid name supplied for building method");
										};
									return TRUE;
								break;
								case "MethodClean":
									if (isset($this->methods[$data[1]]))
										{
											$this->methods[$data[1]] = array();
										}
									else
										{
											$this->error(ERROR_WARNING, $this->DFL_INTERNALS['LNUM'], $this->DFL_INTERNALS['FILE'], "Invalid name supplied for cleaning method");
										};
									return TRUE;
								break;
								case "MethodDestroy":
									if (isset($this->methods[$data[1]]))
										{
											unset($this->methods[$data[1]]);
										}
									else
										{
											$this->error(ERROR_WARNING, $this->DFL_INTERNALS['LNUM'], $this->DFL_INTERNALS['FILE'], "Invalid name supplied for destroying method");
										};
									return TRUE;
								break;
								case "MethodDeclare":
									if (isset($this->methods[$data[1]]))
										{
											$this->safe_parse_line($data[1]);
										}
									else
										{
											$this->error(ERROR_WARNING, $this->DFL_INTERNALS['LNUM'], $this->DFL_INTERNALS['FILE'], "Invalid name supplied for declaring method");
										};
									return TRUE;
								break;
								case "MethodAppend":
									if (isset($this->methods[$data[1]]))
										{
											$data[0] = "";
											$name = $data[1];
											$data[1] = "";
											$data = implode(" ", $data);
											$data = trim($data);
											$this->methods[$name][] = $data;
										}
									else
										{
											$this->error(ERROR_WARNING, $this->DFL_INTERNALS['LNUM'], $this->DFL_INTERNALS['FILE'], "Invalid name supplied for appending to method");
										};
									return TRUE;
								break;
							};
						$this->error(ERROR_ERROR, $this->DFL_INTERNALS['LNUM'], $this->DFL_INTERNALS['FILE'], "Could not declare method; out of bounds");
						return TRUE;
					}
				else
					{
						if (isset($this->methods[$data[0]]))
							{
								if (!empty($this->methods[$data[0]]))
									{
										$data_args = $data;
										$data_args[0] = "";
										$data_args = implode(" ", $data_args);
										$data_args = trim($data_args);
										if (!empty($data_args))
											{
												$data_args = $this->safe_parse_line($data_args);
											};
										$variable_dump = $this->vars;
										$function_dump = $this->funcs;
										$this->vars = array("args" => $data_args);
										$this->funcs = array();
										
										foreach ($this->methods[$data[0]] as $eval)
											{
												$prelevel = $this->DFL_INTERNALS["LEVEL"];
												$this->safe_parse_line($eval);
												if ($this->returned_last === TRUE)
													{
														$this->returned_last = FALSE;
														$this->DFL_INTERNALS["LEVEL"] = $prelevel;
														$this->vars = $variable_dump;
														$this->funcs = $function_dump;
														return TRUE;
													};
												$this->DFL_INTERNALS["LEVEL"] = $prelevel;
											};
										$this->vars = $variable_dump;
										$this->funcs = $function_dump;
										$this->return_content = TRUE;
									};
								return TRUE;
							}
						else
							{
								return FALSE;
							};
					};
			}
	};

if (empty($argv[1]))
	{
		die("No input file");
	};

$debug = FALSE;
$sleep = FALSE;

if ($argv[2] == "--debug")
	{
		$debug = TRUE;
	}
elseif ($argv[2] == "--noend")
	{
		$sleep = TRUE;
	};

if ($argv[3] == "--debug")
	{
		$debug = TRUE;
	}
elseif ($argv[3] == "--noend")
	{
		$sleep = TRUE;
	};

$DFL = new DFL;
$DFL->init($argv[1], $debug, TRUE);

if ($sleep)
	{
		while (1) {};
	};

?>
Return current item: DFL