Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

116
LINES

< > TinyBrain | #1008907 - CtxExpr etc.

JavaX fragment (include)

abstract sclass CtxExpr {
  // assumes that ctxContext() is set
  
  abstract bool get();
  abstract S str();
}

abstract sclass CtxGeneral extends CtxExpr {
  bool get() { ret eqic(str(), "true"); }
}

abstract sclass CtxBool extends CtxExpr {
  S str() { ret get() ? "true" : "false"; }
}

sclass CtxConst extends CtxGeneral {
  S value;
  
  *() {}
  *(S *value) {}
  
  // assume it's a regexp when queried with get()
  // and a string constant when queried with str()
  // (maybe we don't need this trick anymore)
  S str() { ret value; }
  bool get() { ret ctxEvalPattern(value); }
}

sclass CtxVar extends CtxGeneral {
  S var;
  
  *() {}
  *(S *var) {}
  
  S str() { ret ctxGetVar(var); }
}

sclass CtxNeg extends CtxBool {
  CtxExpr e;
  
  *() {}
  *(CtxExpr *e) {}
  
  bool get() { ret !e.get(); }
}

sclass CtxAnd extends CtxBool {  
  CtxExpr a, b;
  
  *() {}
  *(CtxExpr *a, CtxExpr *b) {}

  bool get() { ret a.get() && b.get(); }
}

sclass CtxOr extends CtxBool {  
  CtxExpr a, b;
  
  *() {}
  *(CtxExpr *a, CtxExpr *b) {}
  
  bool get() { ret a.get() || b.get(); }
}

sclass CtxRegexp extends CtxBool {
  S pattern;
  
  *() {}
  *(S *pattern) {}
  
  bool get() { ret ctxEvalPattern(pattern); }
}

sclass CtxComp extends CtxBool {
  CtxExpr a, b;
  
  *() {}
  *(CtxExpr *a, CtxExpr *b) {}
  
  bool get() { ret eq(a.str(), b.str()); }
}

sclass CtxUneq extends CtxBool {
  CtxExpr a, b;
  
  *() {}
  *(CtxExpr *a, CtxExpr *b) {}

  bool get() { ret neq(a.str(), b.str()); }
}

sclass CtxMatch extends CtxBool {
  CtxExpr var;
  S pattern;
  
  *() {}
  *(CtxExpr *var, S *pattern) {}
  
  bool get() { ret ctxEvalPattern(pattern, var.str()); }
}

sclass CtxFCall extends CtxGeneral {
  S fname;
  new L<CtxExpr> args;
  
  *() {}
  *(S *fname) {}
  
  void add(CtxExpr arg) {
    args.add(arg);
  }
  
  S str() {
    ret ctxCallFunction(fname, map(func(CtxExpr e) { e.str() }, args));
  }
}

download  show line numbers  debug dex   

Travelled to 5 computer(s): cfunsshuasjs, gwrvuhgaqvyk, mqqgnosmbjvj, onxytkatvevr, tvejysmllsmz

No comments. add comment

Snippet ID: #1008907
Snippet name: CtxExpr etc.
Eternal ID of this version: #1008907/22
Text MD5: e497c5f75d18cbaca92f9e25c0393e3b
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-02-07 16:09:38
Source code size: 2033 bytes / 116 lines
Pitched / IR pitched: No / No
Views / Downloads: 133 / 492
Version history: 21 change(s)
Referenced in: [show]