Artifact Content
Not logged in

Artifact f8a685431329b03dbf2b34be97eb2942b0460058


     1  /**
     2   * Authors: k.inaba
     3   * License: NYSL 0.9982 http://www.kmonos.net/nysl/
     4   *
     5   * Syntax tree for Polemy programming language.
     6   */
     7  module polemy.ast;
     8  import polemy._common;
     9  import polemy.lex;
    10  
    11  ///
    12  abstract class AST
    13  {
    14  	immutable LexPosition pos;
    15  	mixin SimpleConstructor;
    16  	mixin SimplePatternMatch;
    17  }
    18  
    19  ///
    20  class StrLiteral : AST
    21  {
    22  	string data;
    23  	mixin SimpleClass;
    24  }
    25  
    26  ///
    27  class IntLiteral : AST
    28  {
    29  	BigInt data;
    30  	mixin SimpleClass;
    31  	this(immutable LexPosition pos, long n) {super(pos); data = n;}
    32  	this(immutable LexPosition pos, BigInt n) {super(pos); data = n;}
    33  	this(immutable LexPosition pos, string n) {super(pos); data = BigInt(n);}
    34  }
    35  
    36  ///
    37  class VarExpression : AST
    38  {
    39  	string var;
    40  	mixin SimpleClass;
    41  }
    42  
    43  ///
    44  class LayeredExpression : AST
    45  {
    46  	string lay;
    47  	AST    expr;
    48  	mixin SimpleClass;
    49  }
    50  
    51  ///
    52  class LetExpression : AST
    53  {
    54  	string var;
    55  	string layer;
    56  	AST    init;
    57  	AST    expr;
    58  	mixin SimpleClass;
    59  }
    60  
    61  ///
    62  class FuncallExpression : AST
    63  {
    64  	AST   fun;
    65  	AST[] args;
    66  	this(immutable LexPosition pos, AST fun, AST[] args...)
    67  		{ super(pos); this.fun=fun; this.args=args.dup; }
    68  	mixin SimpleClass;
    69  }
    70  
    71  ///
    72  class Parameter
    73  {
    74  	string   name;
    75  	string[] layers;
    76  	mixin SimpleClass;
    77  }
    78  
    79  ///
    80  class FunLiteral : AST
    81  {
    82  	Parameter[] params;
    83  	AST         funbody;
    84  	mixin SimpleClass;
    85  }
    86  
    87  /// Handy Generator for AST nodes. To use this, mixin EasyAst;
    88  
    89  /*mixin*/
    90  template EasyAST()
    91  {
    92  	///
    93  	template genEast(T)
    94  		{ T genEast(P...)(P ps) { return new T(LexPosition.dummy, ps); } }
    95  
    96  	alias genEast!StrLiteral strl; ///
    97  	alias genEast!IntLiteral intl; ///
    98  	auto fun(string[] xs, AST ps) {
    99  		return genEast!FunLiteral(array(map!((string x){return new Parameter(x,[]);})(xs)),ps); }
   100  	auto funp(Parameter[] xs, AST ps) { return genEast!FunLiteral(xs,ps); } ///
   101  	alias genEast!VarExpression var; ///
   102  	alias genEast!LayeredExpression lay; ///
   103  	alias genEast!LetExpression let; ///
   104  	alias genEast!FuncallExpression call; ///
   105  	auto param(string name, string[] lay...) { return new Parameter(name, lay); } ///
   106  }