Advertisement
tinyevil

Untitled

Jun 29th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. struct ExpressionEmitter{
  2.     method buildI8(self:ref ExpressionEmitter, val:i8):gc[Expression];
  3.     method buildI16(self:ref ExpressionEmitter, val:i16):gc[Expression];
  4.     method buildI32(self:ref ExpressionEmitter, val:i32):gc[Expression];
  5.     method buildI64(self:ref ExpressionEmitter, val:i64):gc[Expression];
  6.     method buildString(self:ref ExpressionEmitter, val:ptr[] u8):gc[Expression];
  7.     method buildCall(self:ref ExpressionEmitter, head:gc[Expression], args:slice[gc[Expression]]):gc[Expression];
  8.     ....
  9. }
  10.  
  11.  
  12. class ToExpression[T]{
  13.     method materialize(emitter:ref ExpressionEmitter, val:ref T):gc[Expression];
  14. }
  15.  
  16. instance ToExpression[i32]{
  17.     method materialize(emitter:ref ExpressionEmitter, val:ref i32):gc[Expression]{
  18.         return emitter.buildI32(*val);
  19.     }
  20. }
  21.  
  22. ...
  23.  
  24. struct MyData{
  25.     field x y:i32;
  26. }
  27.  
  28. instance ToExpression[MyData]{
  29.     method materialize(emitter:ref ExpressionEmitter, val:ref MyData):gc[Expression]{
  30.         let constr = emitter.buildConstructor(reflect(MyData));
  31.         constr.addField(reflect(MyData.x), @val->x);
  32.         constr.addField(reflect(MyData.y), @val->y);
  33.         return constr.toExpression();
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement