Commits

Lenard Lindstrom committed 01f2cbb

Update parser and transform to use new nodes

Comments (0)

Files changed (1)

 # Parent e8eda5d374509c42e47d64197b808ebc8262551a
 Revise ast nodes to support hard-coded blit routines
 
-diff -r e8eda5d37450 -r fcb03033727a include/imgblit/block.hpp
+diff -r e8eda5d37450 include/imgblit/block.hpp
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-+++ b/include/imgblit/block.hpp	Mon Apr 28 21:31:40 2014 -0700
++++ b/include/imgblit/block.hpp	Wed May 14 14:04:45 2014 -0700
 @@ -0,0 +1,55 @@
 +#ifndef IMGBLIT_BLOCK_HPP
 +#define IMGBLIT_BLOCK_HPP
 +};
 +
 +#endif
-diff -r e8eda5d37450 -r fcb03033727a include/imgblit/instruction.hpp
+diff -r e8eda5d37450 include/imgblit/instruction.hpp
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-+++ b/include/imgblit/instruction.hpp	Mon Apr 28 21:31:40 2014 -0700
++++ b/include/imgblit/instruction.hpp	Wed May 14 14:04:45 2014 -0700
 @@ -0,0 +1,66 @@
 +#ifndef IMGBLIT_INSTRUCTION_HPP
 +#define IMGBLIT_INSTRUCTION_HPP
 +};
 +
 +#endif
-diff -r e8eda5d37450 -r fcb03033727a include/imgblit/parser.hpp
+diff -r e8eda5d37450 include/imgblit/parser.hpp
 --- a/include/imgblit/parser.hpp	Wed Apr 23 22:39:41 2014 -0700
-+++ b/include/imgblit/parser.hpp	Mon Apr 28 21:31:40 2014 -0700
++++ b/include/imgblit/parser.hpp	Wed May 14 14:04:45 2014 -0700
 @@ -1,6 +1,7 @@
  #ifndef IMGBLIT_PARSER_HPP
  #define IMGBLIT_PARSER_HPP
      protected:
          Scanner &psr_source;
          reserved_words instruction_set;
-diff -r e8eda5d37450 -r fcb03033727a include/imgblit/value.hpp
+diff -r e8eda5d37450 include/imgblit/transform.hpp
+--- a/include/imgblit/transform.hpp	Wed Apr 23 22:39:41 2014 -0700
++++ b/include/imgblit/transform.hpp	Wed May 14 14:04:45 2014 -0700
+@@ -12,7 +12,7 @@
+     class Transformation {
+ 
+     public:
+-        typedef Block::instructions::iterator i_iterator;
++        typedef Block::InstructionList::iterator i_iterator;
+         typedef void (*elaborator)(BlockRef block,
+                                    i_iterator &where,
+                                    Transformation &t);
+diff -r e8eda5d37450 include/imgblit/value.hpp
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-+++ b/include/imgblit/value.hpp	Mon Apr 28 21:31:40 2014 -0700
++++ b/include/imgblit/value.hpp	Wed May 14 14:04:45 2014 -0700
 @@ -0,0 +1,28 @@
 +#ifndef IMGBLIT_VALUE_HPP
 +#define IMGBLIT_VALUE_HPP
 +};
 +
 +#endif
-diff -r e8eda5d37450 -r fcb03033727a src/block.cpp
+diff -r e8eda5d37450 src/block.cpp
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-+++ b/src/block.cpp	Mon Apr 28 21:31:40 2014 -0700
++++ b/src/block.cpp	Wed May 14 14:04:45 2014 -0700
 @@ -0,0 +1,67 @@
 +#include "imgblit/block.hpp"
 +#include <ostream>
 +    (*this)->v_block->b_instructions.push_back(i);
 +    return *this;
 +}
-diff -r e8eda5d37450 -r fcb03033727a src/instruction.cpp
+diff -r e8eda5d37450 src/instruction.cpp
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-+++ b/src/instruction.cpp	Mon Apr 28 21:31:40 2014 -0700
-@@ -0,0 +1,16 @@
++++ b/src/instruction.cpp	Wed May 14 14:04:45 2014 -0700
+@@ -0,0 +1,19 @@
 +#include "imgblit/instruction.hpp"
 +
 +using namespace imgblit;
 +void
 +Instruction::write(ostream &os, const char *args) const
 +{
-+    os << i_opname.c_str() << ' ' << args;
++    os << i_opname.c_str();
++    if (args[0]) {
++        os << ' ' << args;
++    }
 +}
-diff -r e8eda5d37450 -r fcb03033727a src/parser.cpp
+diff -r e8eda5d37450 src/parser.cpp
 --- a/src/parser.cpp	Wed Apr 23 22:39:41 2014 -0700
-+++ b/src/parser.cpp	Mon Apr 28 21:31:40 2014 -0700
-@@ -125,8 +125,8 @@
++++ b/src/parser.cpp	Wed May 14 14:04:45 2014 -0700
+@@ -13,101 +13,6 @@
+ using std::endl;
+ 
+ 
+-void
+-Node::write(ostream &os) const
+-{
+-    os << "Node(" << nd_identifier.c_str() << ")";
+-}
+-
+-
+-void
+-Value::write(ostream &os) const
+-{
+-    os << v_type << "(" << nd_identifier.c_str() << ")";
+-}
+-
+-
+-void
+-Instruction::write(ostream &os) const
+-{
+-    size_t sz = insn_arguments.size();
+-    size_t i = 0;
+-
+-    os << nd_identifier;
+-    if (sz != 0) {
+-        do {
+-            os << " " << insn_arguments[i];
+-            ++i;
+-        }
+-        while(i != sz);
+-    }
+-}
+-
+-
+-void
+-Block::write(ostream &os) const
+-{
+-    typedef instructions::const_iterator iterator;
+-    iterator end = blk_instructions.end();
+-
+-    for (iterator i = blk_instructions.begin(); i != end; ++i) {
+-        os << *i << endl;
+-    }
+-}
+-
+-
+-namespace imgblit {
+-    InstructionRef &
+-    operator <<(InstructionRef &r, ValueRef arg)
+-    {
+-        if (!!r) {
+-            r->insn_arguments.push_back(arg);
+-        }
+-        return r;
+-    }
+-
+-    InstructionRef &
+-    operator <<(InstructionRef &r, Value *arg)
+-    {
+-        assert(arg);
+-        return r << ValueRef(arg);
+-    }
+-
+-    size_t size(const InstructionRef &r)
+-    {
+-        if (!!r) {
+-            return r->insn_arguments.size();
+-        }
+-        return 0;
+-    }
+-
+-
+-    BlockRef &
+-    operator <<(BlockRef &r, InstructionRef arg)
+-    {
+-        if (!!r) {
+-            r->blk_instructions.push_back(arg);
+-        }
+-        return r;
+-    }
+-
+-    BlockRef &
+-    operator <<(BlockRef &r, Instruction *arg)
+-    {
+-        assert(arg);
+-        return r << InstructionRef(arg);
+-    }
+-
+-    size_t size(const BlockRef &r)
+-    {
+-        if (!!r) {
+-            return r->blk_instructions.size();
+-        }
+-        return 0;
+-    }
+-};
+-
+-
+ Parser::~Parser()
+ {
+ }
+@@ -125,10 +30,9 @@
      return instruction_set[identifier];
  }
  
 +void
 +Parser::parse(BlockRef block)
  {
-     BlockRef block(new Block);
+-    BlockRef block(new Block);
      psr_source.reset();
-diff -r e8eda5d37450 -r fcb03033727a src/value.cpp
+     TokenRef next = psr_source.get_token();
+     instruction_parser parser = 0;
+@@ -146,5 +50,4 @@
+         block << i->second(psr_source);
+         next = psr_source.get_token();
+     }
+-    return block;
+ }
+diff -r e8eda5d37450 src/transform.cpp
+--- a/src/transform.cpp	Wed Apr 23 22:39:41 2014 -0700
++++ b/src/transform.cpp	Wed May 14 14:04:45 2014 -0700
+@@ -24,15 +24,15 @@
+ {
+     e_iterator e;
+     e_iterator e_end = tra_transforms.end();
+-    i_iterator i = block->blk_instructions.begin();
+-    i_iterator i_end = block->blk_instructions.end();
++    i_iterator i = block->b_instructions.begin();
++    i_iterator i_end = block->b_instructions.end();
+ 
+     while (i != i_end) {
+-        e = tra_transforms.find((*i)->nd_identifier);
++        e = tra_transforms.find((*i)->i_opname);
+         if (e == e_end) {
+             stringstream emsg;
+             emsg << "Unknown instruction "
+-                 << "\"" << (*i)->nd_identifier.c_str() << "\".";
++                 << "\"" << (*i)->i_opname.c_str() << "\".";
+             throw ValueError(emsg.str());
+         }
+         e->second(block, i, *this);
+diff -r e8eda5d37450 src/value.cpp
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-+++ b/src/value.cpp	Mon Apr 28 21:31:40 2014 -0700
++++ b/src/value.cpp	Wed May 14 14:04:45 2014 -0700
 @@ -0,0 +1,12 @@
 +#include "imgblit/value.hpp"
 +
 +{
 +    os << v_identifier.c_str() << ':' << v_type;
 +}
-diff -r e8eda5d37450 -r fcb03033727a test/testblock.cpp
+diff -r e8eda5d37450 test/testblock.cpp
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-+++ b/test/testblock.cpp	Mon Apr 28 21:31:40 2014 -0700
++++ b/test/testblock.cpp	Wed May 14 14:04:45 2014 -0700
 @@ -0,0 +1,52 @@
 +#include "imgblit/block.hpp"
 +#include <sstream>
 +                             "MULT 2 x:pixel\n"
 +                             "MOVE x:pixel y:pixel\n"));
 +}
-diff -r e8eda5d37450 -r fcb03033727a test/testparser.cpp
+diff -r e8eda5d37450 test/testparser.cpp
 --- a/test/testparser.cpp	Wed Apr 23 22:39:41 2014 -0700
-+++ b/test/testparser.cpp	Mon Apr 28 21:31:40 2014 -0700
-@@ -58,13 +58,15 @@
++++ b/test/testparser.cpp	Wed May 14 14:04:45 2014 -0700
+@@ -1,8 +1,8 @@
+ #include "imgblit/parser.hpp"
++#include "imgblit/value.hpp"
+ #include <sstream>
+ #include <cassert>
+ 
+-#include <iostream>
+ 
+ using namespace imgblit;
+ using std::stringstream;
+@@ -34,9 +34,10 @@
+ UNARYOP(Scanner &scanner)
+ {
+     const char * const name = "UNARYOP";
+-    InstructionRef i(new Instruction(name));
+-    TokenRef t = scanner.get_token();
+-    i << new Value(t->tk_lexeme, t->tk_class);
++    typedef Instruction1<ValueRef> I1;
++    TokenRef tk_source = scanner.get_token();
++    ValueRef v_source(new Value(tk_source->tk_lexeme, tk_source->tk_class));
++    InstructionRef i(new I1(name, v_source));
+     return i;
+ }
+ 
+@@ -44,11 +45,12 @@
+ BINOP(Scanner &scanner)
+ {
+     const char * const name = "BINOP";
+-    InstructionRef i(new Instruction(name));
+-    TokenRef t = scanner.get_token();
+-    i << new Value(t->tk_lexeme, t->tk_class);
+-    t = scanner.get_token();
+-    i << new Value(t->tk_lexeme, t->tk_class);
++    typedef Instruction2<ValueRef, ValueRef> I2;
++    TokenRef tk_source = scanner.get_token();
++    ValueRef v_source(new Value(tk_source->tk_lexeme, tk_source->tk_class));
++    TokenRef tk_target = scanner.get_token();
++    ValueRef v_target(new Value(tk_target->tk_lexeme, tk_target->tk_class));
++    InstructionRef i(new I2(name, v_source, v_target));
+     return i;
+ }
+ 
+@@ -58,14 +60,16 @@
      const char * const code = "OP UNARYOP abc BINOP def ghi";
      Scanner scanner;
      Parser parser(scanner);
      parser["UNARYOP"] = UNARYOP;
      parser["BINOP"] = BINOP;
 -    capture << parser.parse();
-+    parser.parse(block)
++    parser.parse(block);
 +    capture << block;
      assert(capture.str() == ("OP\n"
-                              "UNARYOP Value(abc)\n"
-                              "BINOP Value(def) Value(ghi)\n"));
-diff -r e8eda5d37450 -r fcb03033727a test/testvalue.cpp
+-                             "UNARYOP Value(abc)\n"
+-                             "BINOP Value(def) Value(ghi)\n"));
++                             "UNARYOP abc:Value\n"
++                             "BINOP def:Value ghi:Value\n"));
+ }
+diff -r e8eda5d37450 test/testtransform.cpp
+--- a/test/testtransform.cpp	Wed Apr 23 22:39:41 2014 -0700
++++ b/test/testtransform.cpp	Wed May 14 14:04:45 2014 -0700
+@@ -1,9 +1,13 @@
+ #include "imgblit/transform.hpp"
++#include "imgblit/value.hpp"
+ #include <sstream>
+ #include <cassert>
++#include <cstdlib>
++
+ 
+ using namespace imgblit;
+ using std::stringstream;
++using std::atoi;
+ 
+ 
+ void test_transformation();
+@@ -34,9 +38,10 @@
+ UNARYOP(Scanner &scanner)
+ {
+     const char * const name = "UNARYOP";
+-    InstructionRef i(new Instruction(name));
+-    TokenRef t = scanner.get_token();
+-    i << new Value(t->tk_lexeme, t->tk_class);
++    typedef Instruction1<ValueRef> I1;
++    TokenRef tk_source = scanner.get_token();
++    ValueRef v_source(new Value(tk_source->tk_lexeme, tk_source->tk_class));
++    InstructionRef i(new I1(name, v_source));
+     return i;
+ }
+ 
+@@ -44,11 +49,12 @@
+ BINOP(Scanner &scanner)
+ {
+     const char * const name = "BINOP";
+-    InstructionRef i(new Instruction(name));
+-    TokenRef t = scanner.get_token();
+-    i << new Value(t->tk_lexeme, t->tk_class);
+-    t = scanner.get_token();
+-    i << new Value(t->tk_lexeme, t->tk_class);
++    typedef Instruction2<int, ValueRef> I2;
++    TokenRef tk_source = scanner.get_token();
++    int i_source = atoi(tk_source->tk_lexeme.c_str());
++    TokenRef tk_target = scanner.get_token();
++    ValueRef v_target(new Value(tk_target->tk_lexeme, tk_target->tk_class));
++    InstructionRef i(new I2(name, i_source, v_target));
+     return i;
+ }
+ 
+@@ -59,7 +65,7 @@
+ {
+     // Simply remove the instruction.
+     Transformation::i_iterator here = where++;
+-    block->blk_instructions.erase(here);
++    block->b_instructions.erase(here);
+ }
+ 
+ void
+@@ -68,11 +74,13 @@
+                   Transformation &t)
+ {
+     // Replace an instruction.
++    typedef Instruction1<ValueRef> I1;
++    typedef Instruction2<int, ValueRef> I2;
+     Transformation::i_iterator here = where++;
+-    InstructionRef i(new Instruction("BINOP"));
+-    i << new Value("0", "Int16") << (*here)->insn_arguments[0];
+-    block->blk_instructions.insert(here, i);
+-    block->blk_instructions.erase(here);
++    I2 *op_p = new I2("BINOP", 0, dynamic_cast<I1 *>(&**here)->i1_target_param);
++    InstructionRef i(op_p);
++    block->b_instructions.insert(here, i);
++    block->b_instructions.erase(here);
+ }
+ 
+ void
+@@ -91,6 +99,7 @@
+     Scanner scan;
+     Parser parse(scan);
+     Transformation transform;
++    BlockRef block(new Block);
+ 
+     scan << a_token;
+     parse["OP"] = OP;
+@@ -100,9 +109,9 @@
+     transform["UNARYOP"] = UNARYOP_transform;
+     transform["BINOP"] = BINOP_transform;
+     scan << "UNARYOP x OP BINOP 3 y OP";
+-    BlockRef block = parse.parse();
++    parse.parse(block);
+     transform.transform(block);
+     capture << block;
+-    assert(capture.str() == ("BINOP Int16(0) Value(x)\n"
+-                             "BINOP Value(3) Value(y)\n"));
++    assert(capture.str() == ("BINOP 0 x:Value\n"
++                             "BINOP 3 y:Value\n"));
+ }
+diff -r e8eda5d37450 test/testvalue.cpp
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-+++ b/test/testvalue.cpp	Mon Apr 28 21:31:40 2014 -0700
++++ b/test/testvalue.cpp	Wed May 14 14:04:45 2014 -0700
 @@ -0,0 +1,37 @@
 +#include "imgblit/value.hpp"
 +#include <string>