Skip to content

Instantly share code, notes, and snippets.

@dabroz
Created April 16, 2011 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dabroz/923509 to your computer and use it in GitHub Desktop.
Save dabroz/923509 to your computer and use it in GitHub Desktop.
<?php
printf("properties: %s -> %s\n", $argv[1], $argv[2]);
$text = file_get_contents($argv[1]);
function find_close($text, $start)
{
$i = $start + 1;
$level = 1;
while (true)
{
$c = $text[$i];
if ($c == '{') $level++;
else if ($c == '}')
{
$level--;
if ($level == 0)
{
return $i;
}
}
$i++;
}
return -1;
}
function create_property2($class, $name, $type, $get, $set)
{
$that = '(('.$class.'*)((char*)this - offsetof('.$class.', '.$name.')))';
$code = '';
$code .= "$type {$name}_getter() $get\n";
$code .= "void {$name}_setter($type value) $set\n";
$code .= 'PPCC property_'.$name."\n{\n";
$code .= "int padding;\npublic:\n";
$code .= "operator $type () { return $that->{$name}_getter(); }\n";
$code .= "$type & operator = ($type value) { $that->{$name}_setter(value); }";
$code .= "} $name;";
return $code;
}
function create_property($class, $property)
{
$p = trim(substr($property, strlen('@property')));
$i = strpos($p, ' ');
$type = trim(substr($p, 0, $i));
$p = trim(substr($p, $i));
$i = strpos($p, ' ');
$name = trim(substr($p, 0, $i));
$body = trim(substr($p, $i));
$i = strpos($body, 'get');
$open = strpos($body, '{', $i);
$close = find_close($body, $open) + 1;
$get = substr($body, $open, $close - $open);
$i = strpos($body, 'set');
$open = strpos($body, '{', $i);
$close = find_close($body, $open) + 1;
$set = substr($body, $open, $close - $open);
return create_property2($class, $name, $type, $get, $set);
}
while (true)
{
$i = strpos($text, '@property');
if ($i === false) break;
$class = strrpos($text, 'class', $i-strlen($text));
$class = trim(substr($text, $class+strlen('class')));
$class = trim(substr($class, 0, strpos($class, "\n")));
$open = strpos($text, '{', $i);
$close = find_close($text, $open)+1;
$property = substr($text, $i, $close - $i + 1);
$text = substr($text, 0, $i) . create_property($class, trim($property)) . substr($text, $close);
}
$text = str_replace('PPCC', 'class', $text);
file_put_contents($argv[2], $text);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment