#------------------------------------------------------------------------------
# VectorLiteral
-foo = []
-foo = [1]
-foo = [1,2,3]
-foo = foo[1]
-foo = [1,2,3,4,5][2]
+#foo = []
+#foo = [1]
+#foo = [1,2,3]
+#foo = foo[1]
+#foo = [1,2,3,4,5][2]
# ListLiteral
-foo = ()
-foo = (1,2,3)
-foo = foo[1]
-foo = (1,2,3,4,5)[2]
+#foo = ()
+#foo = (1,2,3)
+#foo = foo[1]
+#foo = (1,2,3,4,5)[2]
# FuncLiteral
foo = { 1 + 1 }
-foo = {|a| a + 1}
-foo = {|a,b| a + b }
-foo = foo(1,2)
-foo = ({|a,b| a + b })(1,2)
+#foo = {|a| a + 1}
+#foo = {|a,b| a + b }
+#foo = foo(1,2)
+#foo = ({|a,b| a + b })(1,2)
# ID
-foo = bar
-foo.bar = bar
+#foo = bar
+#foo.bar = bar
# NUM
-foo = 1
-foo = 1.0
+#foo = 1
+#foo = 1.0
# CHAR
-foo = 'a'
+#foo = 'a'
# STRING
-foo = "some string"
-foo = "12345"[2]
+#foo = "some string"
+#foo = "12345"[2]
# SYMBOL
-foo = $some_symbol
+#foo = $some_symbol
# MAP
-foo = {
- $foo : 1 + 1,
- $bar : 2 + 2,
- $stuff : 3 + 3
-}
+#foo = {
+# $foo : 1 + 1,
+# $bar : 2 + 2,
+# $stuff : 3 + 3
+#}
# Macro
-% if [
- (Expression Block Block) : exec_if($1, $2, $3),
- (Expression Block) : exec_if($1, $2)
-]
+#% if [
+# (Expression Block Block) : exec_if($1, $2, $3),
+# (Expression Block) : exec_if($1, $2)
+#]
-if (1==1)
-{
- 1 + 1
-}{
-
-}
-
-if (1 == 1)
-{
-
-}
+#if (1==1)
+#{
+# 1 + 1
+#}{
+#
+#}
+#
+#if (1 == 1)
+#{
+#
+#}
AST* DLParser::MacroPatternMatch(Pattern patt)
{
+ AST* ret = NULL;
std::vector<AST*> params;
std::list<PatternType_T>::iterator patt_it;
+
for(patt_it = patt.begin(); patt_it != patt.end(); patt_it++)
{
AST* param = NULL;
throw Exception(lookaheadToken(1).line(), lookaheadToken(1).column());
break;
}
- params.push_back(param);
+
+ if( !isSpeculating() )
+ {
+ params.push_back(param);
+ }
+ else
+ {
+ delete param;
+ }
}
- return patt.accept( params );
+ ret = (isSpeculating()) ? _new AST(MACRO) : patt.accept( params );
+
+ return ret;
}
bool DLParser::speculate_GroupExpr(void)
{
std::string text = lookaheadToken(1).text();
match(ID);
- //pattern.push_back( str_to_patt_type(text) );
if( pattern_types.find(text) != pattern_types.end() )
{
pattern.push_back( pattern_types[ text ] );
#include "pattern.h"
+#include "dllexer.h"
Pattern::Pattern(const std::list<PatternType_T>& patt, const AST* ast) : pattern(patt), expr_ast(ast)
{
return pattern.end();
}
+void Pattern::apply(AST* cur,std::vector<AST*>& params)
+{
+ if (cur != NULL)
+ {
+ list<AST*>* children = cur->children();
+ list<AST*>::iterator it = children->begin();
+
+ // Visit the tree
+ for(; it != children->end(); it++)
+ {
+ if ((*it)->type() == SYMBOL)
+ {
+ unsigned int arg;
+ istringstream((*it)->text()) >> arg;
+
+ if (arg <= params.size())
+ {
+ AST* temp = *it;
+ *it = params[ arg - 1 ];
+ delete temp;
+ }
+ else
+ {
+ throw "Invalid parameter number";
+ }
+ }
+ else
+ {
+ apply( *it, params );
+ }
+ }
+ }
+}
+
AST* Pattern::accept(std::vector<AST*>& params)
{
AST* ret = expr_ast->clone();
- // TODO: Perform AST substitution
- // if size == to expected
- // clone exp tree
- // replace symbols with params
- // else
- // Throw exception
- // endif
+ apply( ret, params );
return ret;
}
} PatternType_T;
class Pattern {
- private:
+ protected:
std::list<PatternType_T> pattern;
const AST* expr_ast;
+ private:
+ void apply(AST* cur,std::vector<AST*>& params);
public:
Pattern(const std::list<PatternType_T>& patt, const AST* ast);
~Pattern();