aa
com.cliffc.aa.HM.HM6 Class Reference
Collaboration diagram for com.cliffc.aa.HM.HM6:
[legend]

Classes

class  Apply
 
class  Con
 
class  Ident
 
class  Lambda
 
class  Lambda2
 
class  Let
 
class  Syntax
 
class  T2
 
class  VStack
 
class  Worklist
 

Public Member Functions

String toString ()
 

Static Public Member Functions

static T2 hm (String sprog)
 

Static Package Functions

static Syntax parse (String s)
 
static void reset ()
 
static Syntax term ()
 

Static Package Attributes

static boolean DEBUG_LEAKS =false
 
static final HashMap< String, T2PRIMS = new HashMap<>()
 

Static Private Member Functions

static String id ()
 
static boolean isAlpha0 (byte c)
 
static boolean isAlpha1 (byte c)
 
static boolean isDigit (byte c)
 
static boolean isWS (byte c)
 
static Syntax number ()
 
static< T > T require (char c, T t)
 
static void require (String s)
 
static byte skipWS ()
 
static Syntax string ()
 

Static Private Attributes

static byte[] BUF
 
static final SB ID = new SB()
 
static int X
 

Detailed Description

Definition at line 23 of file HM6.java.

Member Function Documentation

◆ hm()

static T2 com.cliffc.aa.HM.HM6.hm ( String  sprog)
static

Definition at line 27 of file HM6.java.

27  {
28  Worklist work = new Worklist();
29 
30  // Simple types
31  T2 bool = T2.make_base(TypeInt.BOOL);
32  T2 int64 = T2.make_base(TypeInt.INT64);
33  T2 flt64 = T2.make_base(TypeFlt.FLT64);
34  T2 strp = T2.make_base(TypeMemPtr.STRPTR);
35 
36  // Primitives
37  T2 var1 = T2.make_leaf();
38  T2 var2 = T2.make_leaf();
39 
40  PRIMS.put("pair1",T2.make_fun(var1, T2.make_fun(var2, T2.prim("pair",var1,var2) )));
41  PRIMS.put("pair",T2.make_fun(var1, var2, T2.prim("pair",var1,var2) ));
42 
43  PRIMS.put("if/else",T2.make_fun(bool,var1,var1,var1));
44 
45  PRIMS.put("dec",T2.make_fun(int64,int64));
46  PRIMS.put("*" ,T2.make_fun(int64,int64,int64));
47  PRIMS.put("==0",T2.make_fun(int64,bool));
48  PRIMS.put("isempty",T2.make_fun(strp,bool));
49 
50  // Print a string; int->str
51  PRIMS.put("str",T2.make_fun(int64,strp));
52  // Factor; FP div/mod-like operation
53  PRIMS.put("factor",T2.make_fun(flt64,T2.prim("divmod",flt64,flt64)));
54 
55  // Parse
56  Syntax prog = parse( sprog );
57 
58  // Prep for SSA: pre-gather all the (unique) ids
59  int cnt_syns = prog.prep_tree(null,null,work);
60  int init_T2s = T2.CNT;
61 
62  int cnt=0, DEBUG_CNT=-1;
63  while( work.len()>0 ) { // While work
64  int oldcnt = T2.CNT; // Used for cost-check when no-progress
65  Syntax syn = work.pop(); // Get work
66  T2 old = syn._t; // Old value for progress assert
67  if( cnt==DEBUG_CNT )
68  System.out.println("break here");
69  if( syn.hm(work) ) { // Compute a new HM type and check for progress
70  assert !syn.debug_find().unify(old.find(),null);// monotonic: unifying with the result is no-progress
71  syn.add_kids(work); // Push children on worklist
72  syn.add_occurs(work); // Push occurs-check ids on worklist
73  if( syn._par !=null ) work.push(syn._par); // Parent updates
74  } else {
75  assert !DEBUG_LEAKS || oldcnt==T2.CNT; // No-progress consumes no-new-T2s
76  }
77  // VERY EXPENSIVE ASSERT: O(n^2). Every Syntax that makes progress is on the worklist
78  //assert prog.more_work(work);
79  cnt++;
80  }
81  assert prog.more_work(work);
82 
83  //System.out.println("Initial T2s: "+init_T2s+", Prog size: "+cnt_syns+", worklist iters: "+cnt+", T2s: "+T2.CNT);
84  return prog._t;
85  }

References com.cliffc.aa.HM.HM6.Syntax._par, com.cliffc.aa.HM.HM6.Syntax._t, com.cliffc.aa.HM.HM6.Syntax.add_kids(), com.cliffc.aa.HM.HM6.Syntax.add_occurs(), com.cliffc.aa.type.TypeInt.BOOL, com.cliffc.aa.HM.HM6.T2.CNT, com.cliffc.aa.HM.HM6.Syntax.debug_find(), com.cliffc.aa.HM.HM6.DEBUG_LEAKS, com.cliffc.aa.HM.HM6.T2.find(), com.cliffc.aa.type.TypeFlt.FLT64, com.cliffc.aa.HM.HM6.Syntax.hm(), com.cliffc.aa.type.TypeInt.INT64, com.cliffc.aa.HM.HM6.Worklist.len(), com.cliffc.aa.HM.HM6.T2.make_base(), com.cliffc.aa.HM.HM6.T2.make_fun(), com.cliffc.aa.HM.HM6.T2.make_leaf(), com.cliffc.aa.HM.HM6.Syntax.more_work(), com.cliffc.aa.HM.HM6.parse(), com.cliffc.aa.HM.HM6.Worklist.pop(), com.cliffc.aa.HM.HM6.Syntax.prep_tree(), com.cliffc.aa.HM.HM6.T2.prim(), com.cliffc.aa.HM.HM6.PRIMS, com.cliffc.aa.HM.HM6.Worklist.push(), com.cliffc.aa.type.TypeMemPtr.STRPTR, and com.cliffc.aa.HM.HM6.T2.unify().

Referenced by com.cliffc.aa.HM.TestHM6.test00(), com.cliffc.aa.HM.TestHM6.test01(), com.cliffc.aa.HM.TestHM6.test02(), com.cliffc.aa.HM.TestHM6.test03(), com.cliffc.aa.HM.TestHM6.test04(), com.cliffc.aa.HM.TestHM6.test05(), com.cliffc.aa.HM.TestHM6.test06(), com.cliffc.aa.HM.TestHM6.test07(), com.cliffc.aa.HM.TestHM6.test08(), com.cliffc.aa.HM.TestHM6.test09(), com.cliffc.aa.HM.TestHM6.test10(), com.cliffc.aa.HM.TestHM6.test11(), com.cliffc.aa.HM.TestHM6.test12(), com.cliffc.aa.HM.TestHM6.test13(), com.cliffc.aa.HM.TestHM6.test14(), com.cliffc.aa.HM.TestHM6.test15(), com.cliffc.aa.HM.TestHM6.test16(), com.cliffc.aa.HM.TestHM6.test17(), com.cliffc.aa.HM.TestHM6.test18(), com.cliffc.aa.HM.TestHM6.test19(), com.cliffc.aa.HM.TestHM6.test20(), com.cliffc.aa.HM.TestHM6.test21(), com.cliffc.aa.HM.TestHM6.test22(), and com.cliffc.aa.HM.TestHM6.test23().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ id()

static String com.cliffc.aa.HM.HM6.id ( )
staticprivate

Definition at line 135 of file HM6.java.

135  {
136  ID.clear();
137  while( X<BUF.length && isAlpha1(BUF[X]) )
138  ID.p((char)BUF[X++]);
139  return ID.toString().intern();
140  }

References com.cliffc.aa.HM.HM6.BUF, com.cliffc.aa.util.SB.clear(), com.cliffc.aa.HM.HM6.ID, com.cliffc.aa.HM.HM6.isAlpha1(), com.cliffc.aa.util.SB.p(), com.cliffc.aa.util.SB.toString(), and com.cliffc.aa.HM.HM6.X.

Referenced by com.cliffc.aa.HM.HM6.term().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isAlpha0()

static boolean com.cliffc.aa.HM.HM6.isAlpha0 ( byte  c)
staticprivate

Definition at line 162 of file HM6.java.

162 { return ('a'<=c && c <= 'z') || ('A'<=c && c <= 'Z') || (c=='_') || (c=='*') || (c=='='); }

Referenced by com.cliffc.aa.HM.HM6.isAlpha1(), and com.cliffc.aa.HM.HM6.term().

Here is the caller graph for this function:

◆ isAlpha1()

static boolean com.cliffc.aa.HM.HM6.isAlpha1 ( byte  c)
staticprivate

Definition at line 163 of file HM6.java.

163 { return isAlpha0(c) || ('0'<=c && c <= '9') || (c=='/'); }

References com.cliffc.aa.HM.HM6.isAlpha0().

Referenced by com.cliffc.aa.HM.HM6.id().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isDigit()

static boolean com.cliffc.aa.HM.HM6.isDigit ( byte  c)
staticprivate

Definition at line 161 of file HM6.java.

161 { return '0' <= c && c <= '9'; }

Referenced by com.cliffc.aa.HM.HM6.number(), and com.cliffc.aa.HM.HM6.term().

Here is the caller graph for this function:

◆ isWS()

static boolean com.cliffc.aa.HM.HM6.isWS ( byte  c)
staticprivate

Definition at line 160 of file HM6.java.

160 { return c == ' ' || c == '\t' || c == '\n' || c == '\r'; }

Referenced by com.cliffc.aa.HM.HM6.skipWS().

Here is the caller graph for this function:

◆ number()

static Syntax com.cliffc.aa.HM.HM6.number ( )
staticprivate

Definition at line 141 of file HM6.java.

141  {
142  int sum=0;
143  while( X<BUF.length && isDigit(BUF[X]) )
144  sum = sum*10+BUF[X++]-'0';
145  if( X>= BUF.length || BUF[X]!='.' ) return new Con(TypeInt.con(sum));
146  X++;
147  float f = (float)sum;
148  f = f + (BUF[X++]-'0')/10.0f;
149  return new Con(TypeFlt.con(f));
150  }

References com.cliffc.aa.HM.HM6.BUF, com.cliffc.aa.type.TypeFlt.con(), com.cliffc.aa.type.TypeInt.con(), com.cliffc.aa.HM.HM6.isDigit(), and com.cliffc.aa.HM.HM6.X.

Referenced by com.cliffc.aa.HM.HM6.term().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse()

static Syntax com.cliffc.aa.HM.HM6.parse ( String  s)
staticpackage

Definition at line 93 of file HM6.java.

93  {
94  X = 0;
95  BUF = s.getBytes();
96  Syntax prog = term();
97  if( skipWS() != -1 ) throw unimpl("Junk at end of program: "+new String(BUF,X,BUF.length-X));
98  return prog;
99  }

References com.cliffc.aa.HM.HM6.BUF, com.cliffc.aa.HM.HM6.skipWS(), com.cliffc.aa.HM.HM6.term(), and com.cliffc.aa.HM.HM6.X.

Referenced by com.cliffc.aa.HM.HM6.hm().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ require() [1/2]

static <T> T com.cliffc.aa.HM.HM6.require ( char  c,
t 
)
staticprivate

Definition at line 164 of file HM6.java.

164 { if( skipWS()!=c ) throw unimpl("Missing '"+c+"'"); X++; return t; }

References com.cliffc.aa.HM.HM6.skipWS(), and com.cliffc.aa.HM.HM6.X.

Referenced by com.cliffc.aa.HM.HM6.string(), and com.cliffc.aa.HM.HM6.term().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ require() [2/2]

static void com.cliffc.aa.HM.HM6.require ( String  s)
staticprivate

Definition at line 165 of file HM6.java.

165  {
166  skipWS();
167  for( int i=0; i<s.length(); i++ )
168  if( X+i >= BUF.length || BUF[X+i]!=s.charAt(i) )
169  throw unimpl("Missing '"+s+"'");
170  X+=s.length();
171  }

References com.cliffc.aa.HM.HM6.BUF, com.cliffc.aa.HM.HM6.skipWS(), and com.cliffc.aa.HM.HM6.X.

Here is the call graph for this function:

◆ reset()

static void com.cliffc.aa.HM.HM6.reset ( )
staticpackage

Definition at line 86 of file HM6.java.

86 { PRIMS.clear(); T2.reset(); }

References com.cliffc.aa.HM.HM6.PRIMS, and com.cliffc.aa.HM.HM6.T2.reset().

Here is the call graph for this function:

◆ skipWS()

static byte com.cliffc.aa.HM.HM6.skipWS ( )
staticprivate

Definition at line 156 of file HM6.java.

156  {
157  while( X<BUF.length && isWS(BUF[X]) ) X++;
158  return X==BUF.length ? -1 : BUF[X];
159  }

References com.cliffc.aa.HM.HM6.BUF, com.cliffc.aa.HM.HM6.isWS(), and com.cliffc.aa.HM.HM6.X.

Referenced by com.cliffc.aa.HM.HM6.parse(), com.cliffc.aa.HM.HM6.require(), and com.cliffc.aa.HM.HM6.term().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ string()

static Syntax com.cliffc.aa.HM.HM6.string ( )
staticprivate

Definition at line 151 of file HM6.java.

151  {
152  int start = ++X;
153  while( X<BUF.length && BUF[X]!='"' ) X++;
154  return require('"', new Con(TypeStr.con(new String(BUF,start,X-start).intern())));
155  }

References com.cliffc.aa.HM.HM6.BUF, com.cliffc.aa.type.TypeStr.con(), com.cliffc.aa.HM.HM6.require(), and com.cliffc.aa.HM.HM6.X.

Referenced by com.cliffc.aa.HM.HM6.term().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ term()

static Syntax com.cliffc.aa.HM.HM6.term ( )
staticpackage

Definition at line 100 of file HM6.java.

100  {
101  if( skipWS()==-1 ) return null;
102  if( isDigit(BUF[X]) ) return number();
103  if( BUF[X]=='"' ) return string();
104  if( BUF[X]=='(' ) { // Parse an Apply
105  X++; // Skip paren
106  Syntax fun = term();
107  Ary<Syntax> ARGS = new Ary<>(new Syntax[1],0);
108  while( skipWS()!= ')' && X<BUF.length ) ARGS.push(term());
109  return new Apply(fun,require(')',ARGS.asAry()));
110  }
111  if( BUF[X]=='{' ) { // Lambda of 1 or 2 args
112  X++; // Skip paren
113  skipWS();
114  String arg0 = id(), arg1=null;
115  if( skipWS()!='-' ) arg1 = id();
116  require("->");
117  Syntax body = require('}',term());
118  return arg1==null
119  ? new Lambda (arg0, body)
120  : new Lambda2(arg0,arg1,body);
121  }
122  // Let or Id
123  if( isAlpha0(BUF[X]) ) {
124  String id = id();
125  if( skipWS()!='=' ) return new Ident(id);
126  // Let expression; "id = term(); term..."
127  X++; // Skip '='
128  Syntax def = require(';',term());
129  return new Let(id,def,term());
130  }
131 
132  throw unimpl();
133  }

References com.cliffc.aa.util.Ary< E >.asAry(), com.cliffc.aa.HM.HM6.BUF, com.cliffc.aa.HM.HM6.id(), com.cliffc.aa.HM.HM6.isAlpha0(), com.cliffc.aa.HM.HM6.isDigit(), com.cliffc.aa.HM.HM6.number(), com.cliffc.aa.util.Ary< E >.push(), com.cliffc.aa.HM.HM6.require(), com.cliffc.aa.HM.HM6.skipWS(), com.cliffc.aa.HM.HM6.string(), and com.cliffc.aa.HM.HM6.X.

Referenced by com.cliffc.aa.HM.HM6.parse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toString()

String com.cliffc.aa.HM.HM6.toString ( )

Definition at line 92 of file HM6.java.

92 { return new String(BUF,X,BUF.length-X); }

References com.cliffc.aa.HM.HM6.BUF, and com.cliffc.aa.HM.HM6.X.

Member Data Documentation

◆ BUF

◆ DEBUG_LEAKS

boolean com.cliffc.aa.HM.HM6.DEBUG_LEAKS =false
staticpackage

Definition at line 25 of file HM6.java.

Referenced by com.cliffc.aa.HM.HM6.T2.fresh_unify(), and com.cliffc.aa.HM.HM6.hm().

◆ ID

final SB com.cliffc.aa.HM.HM6.ID = new SB()
staticprivate

Definition at line 134 of file HM6.java.

Referenced by com.cliffc.aa.HM.HM6.id().

◆ PRIMS

final HashMap<String,T2> com.cliffc.aa.HM.HM6.PRIMS = new HashMap<>()
staticpackage

◆ X


The documentation for this class was generated from the following file:
com.cliffc.aa.HM.HM6.DEBUG_LEAKS
static boolean DEBUG_LEAKS
Definition: HM6.java:25
com.cliffc.aa.HM.HM6.BUF
static byte[] BUF
Definition: HM6.java:91
com.cliffc.aa.HM.HM6.parse
static Syntax parse(String s)
Definition: HM6.java:93
com.cliffc.aa.util.Ary.push
E push(E e)
Add element in amortized constant time.
Definition: Ary.java:58
com.cliffc.aa.type.TypeInt
Definition: TypeInt.java:9
com.cliffc.aa.util.SB.clear
SB clear()
Definition: SB.java:61
com.cliffc.aa.type.TypeFlt
Definition: TypeFlt.java:9
com.cliffc.aa.util.Ary
Definition: Ary.java:11
com.cliffc.aa.type.TypeInt.con
static TypeInt con(long con)
Definition: TypeInt.java:37
com.cliffc.aa.HM.HM6.skipWS
static byte skipWS()
Definition: HM6.java:156
com.cliffc.aa.util.Ary.asAry
E[] asAry()
Definition: Ary.java:172
com.cliffc.aa.type.TypeInt.INT64
static final TypeInt INT64
Definition: TypeInt.java:39
com.cliffc.aa.HM.HM6.isAlpha0
static boolean isAlpha0(byte c)
Definition: HM6.java:162
com.cliffc.aa.type.TypeFlt.con
static Type con(double con)
Definition: TypeFlt.java:36
com.cliffc.aa.HM.HM6.id
static String id()
Definition: HM6.java:135
com.cliffc.aa.HM.HM6.isAlpha1
static boolean isAlpha1(byte c)
Definition: HM6.java:163
com.cliffc.aa.HM.HM6.require
static< T > T require(char c, T t)
Definition: HM6.java:164
com.cliffc.aa.HM.HM6.number
static Syntax number()
Definition: HM6.java:141
com.cliffc.aa.HM.HM6.term
static Syntax term()
Definition: HM6.java:100
com.cliffc.aa.type.TypeStr.con
static TypeStr con(String con)
Definition: TypeStr.java:42
com.cliffc.aa.type.TypeStr
Definition: TypeStr.java:14
com.cliffc.aa.HM.HM6.isDigit
static boolean isDigit(byte c)
Definition: HM6.java:161
com.cliffc.aa.HM.HM6.isWS
static boolean isWS(byte c)
Definition: HM6.java:160
com.cliffc.aa.HM.HM6.PRIMS
static final HashMap< String, T2 > PRIMS
Definition: HM6.java:24
com.cliffc.aa.util.SB.p
SB p(String s)
Definition: SB.java:13
com.cliffc.aa.HM.HM6.string
static Syntax string()
Definition: HM6.java:151
com.cliffc.aa.HM.HM6.ID
static final SB ID
Definition: HM6.java:134
com.cliffc.aa.type.TypeMemPtr.STRPTR
static final TypeMemPtr STRPTR
Definition: TypeMemPtr.java:97
com.cliffc.aa.type.TypeInt.BOOL
static final TypeInt BOOL
Definition: TypeInt.java:43
com.cliffc.aa.util.SB.toString
String toString()
Definition: SB.java:62
com.cliffc.aa.HM.HM6.X
static int X
Definition: HM6.java:90
com.cliffc.aa.type.TypeMemPtr
Definition: TypeMemPtr.java:14
com.cliffc.aa.type.TypeFlt.FLT64
static final TypeFlt FLT64
Definition: TypeFlt.java:38