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

Classes

class  Apply
 
class  Con
 
class  Field
 
class  Ident
 
class  Lambda
 
class  Let
 
class  Struct
 
class  Syntax
 
class  T2
 
class  VStack
 
class  Worklist
 

Public Member Functions

String toString ()
 

Static Public Member Functions

static Syntax hm (String sprog)
 

Static Package Functions

 [static initializer]
 
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 24 of file HM8.java.

Member Function Documentation

◆ [static initializer]()

com.cliffc.aa.HM.HM8.[static initializer]
staticpackage

◆ hm()

static Syntax com.cliffc.aa.HM.HM8.hm ( String  sprog)
static

Definition at line 29 of file HM8.java.

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

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

Referenced by com.cliffc.aa.HM.TestHM8.test00(), com.cliffc.aa.HM.TestHM8.test01(), com.cliffc.aa.HM.TestHM8.test02(), com.cliffc.aa.HM.TestHM8.test03(), com.cliffc.aa.HM.TestHM8.test04(), com.cliffc.aa.HM.TestHM8.test05(), com.cliffc.aa.HM.TestHM8.test05a(), com.cliffc.aa.HM.TestHM8.test06(), com.cliffc.aa.HM.TestHM8.test07(), com.cliffc.aa.HM.TestHM8.test08(), com.cliffc.aa.HM.TestHM8.test09(), com.cliffc.aa.HM.TestHM8.test10(), com.cliffc.aa.HM.TestHM8.test11(), com.cliffc.aa.HM.TestHM8.test12(), com.cliffc.aa.HM.TestHM8.test13(), com.cliffc.aa.HM.TestHM8.test14(), com.cliffc.aa.HM.TestHM8.test15(), com.cliffc.aa.HM.TestHM8.test16(), com.cliffc.aa.HM.TestHM8.test17(), com.cliffc.aa.HM.TestHM8.test18(), com.cliffc.aa.HM.TestHM8.test19(), com.cliffc.aa.HM.TestHM8.test20(), com.cliffc.aa.HM.TestHM8.test21(), com.cliffc.aa.HM.TestHM8.test22(), com.cliffc.aa.HM.TestHM8.test23(), com.cliffc.aa.HM.TestHM8.test24(), com.cliffc.aa.HM.TestHM8.test25(), com.cliffc.aa.HM.TestHM8.test25a(), com.cliffc.aa.HM.TestHM8.test25b(), com.cliffc.aa.HM.TestHM8.test26(), com.cliffc.aa.HM.TestHM8.test27(), com.cliffc.aa.HM.TestHM8.test28(), com.cliffc.aa.HM.TestHM8.test29(), com.cliffc.aa.HM.TestHM8.test30(), com.cliffc.aa.HM.TestHM8.test30a(), com.cliffc.aa.HM.TestHM8.test31(), com.cliffc.aa.HM.TestHM8.test32(), com.cliffc.aa.HM.TestHM8.test33(), com.cliffc.aa.HM.TestHM8.test34(), com.cliffc.aa.HM.TestHM8.test35(), and com.cliffc.aa.HM.TestHM8.test36().

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

◆ id()

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

Definition at line 155 of file HM8.java.

155  {
156  ID.clear();
157  while( X<BUF.length && isAlpha1(BUF[X]) )
158  ID.p((char)BUF[X++]);
159  String s = ID.toString().intern();
160  if( s.length()==0 ) throw unimpl("Missing id");
161  return s;
162  }

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

Referenced by com.cliffc.aa.HM.HM8.T2.add_fld(), com.cliffc.aa.HM.HM8.Field.Field(), and com.cliffc.aa.HM.HM8.term().

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

◆ isAlpha0()

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

Definition at line 184 of file HM8.java.

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

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

Here is the caller graph for this function:

◆ isAlpha1()

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

Definition at line 185 of file HM8.java.

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

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

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

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

◆ isDigit()

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

Definition at line 183 of file HM8.java.

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

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

Here is the caller graph for this function:

◆ isWS()

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

Definition at line 182 of file HM8.java.

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

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

Here is the caller graph for this function:

◆ number()

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

Definition at line 163 of file HM8.java.

163  {
164  int sum=0;
165  while( X<BUF.length && isDigit(BUF[X]) )
166  sum = sum*10+BUF[X++]-'0';
167  if( X>= BUF.length || BUF[X]!='.' ) return new Con(TypeInt.con(sum));
168  X++;
169  float f = (float)sum;
170  f = f + (BUF[X++]-'0')/10.0f;
171  return new Con(TypeFlt.con(f));
172  }

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

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

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

◆ parse()

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

Definition at line 93 of file HM8.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.HM8.BUF, com.cliffc.aa.HM.HM8.skipWS(), com.cliffc.aa.HM.HM8.term(), and com.cliffc.aa.HM.HM8.X.

Referenced by com.cliffc.aa.HM.HM8.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.HM8.require ( char  c,
t 
)
staticprivate

Definition at line 186 of file HM8.java.

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

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

Referenced by com.cliffc.aa.HM.HM8.string(), and com.cliffc.aa.HM.HM8.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.HM8.require ( String  s)
staticprivate

Definition at line 187 of file HM8.java.

187  {
188  skipWS();
189  for( int i=0; i<s.length(); i++ )
190  if( X+i >= BUF.length || BUF[X+i]!=s.charAt(i) )
191  throw unimpl("Missing '"+s+"'");
192  X+=s.length();
193  }

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

Here is the call graph for this function:

◆ reset()

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

Definition at line 86 of file HM8.java.

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

References com.cliffc.aa.HM.HM8.PRIMS, com.cliffc.aa.HM.HM8.T2.reset(), and com.cliffc.aa.type.BitsAlias.reset_to_init0().

Referenced by com.cliffc.aa.HM.TestHM8.reset().

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

◆ skipWS()

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

Definition at line 178 of file HM8.java.

178  {
179  while( X<BUF.length && isWS(BUF[X]) ) X++;
180  return X==BUF.length ? -1 : BUF[X];
181  }

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

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

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

◆ string()

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

Definition at line 173 of file HM8.java.

173  {
174  int start = ++X;
175  while( X<BUF.length && BUF[X]!='"' ) X++;
176  return require('"', new Con(TypeStr.con(new String(BUF,start,X-start).intern())));
177  }

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

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

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

◆ term()

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

Definition at line 100 of file HM8.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  Ary<String> args = new Ary<>(new String[1],0);
114  while( skipWS()!='-' ) args.push(id());
115  require("->");
116  Syntax body = require('}',term());
117  return new Lambda(body,args.asAry());
118  }
119  // Let or Id
120  if( isAlpha0(BUF[X]) ) {
121  String id = id();
122  if( skipWS()!='=' ) return new Ident(id);
123  // Let expression; "id = term(); term..."
124  X++; // Skip '='
125  Syntax def = require(';',term());
126  return new Let(id,def,term());
127  }
128 
129  // Structure
130  if( BUF[X]=='@' ) {
131  X++;
132  require('{',null);
133  Ary<String> ids = new Ary<>(String.class);
134  Ary<Syntax> flds = new Ary<>(Syntax.class);
135  while( skipWS()!='}' && X < BUF.length ) {
136  String id = require('=',id());
137  Syntax fld = term();
138  if( fld==null ) throw unimpl("Missing term for field "+id);
139  ids .push( id);
140  flds.push(fld);
141  if( skipWS()==',' ) X++;
142  }
143  return require('}',new Struct(ids.asAry(),flds.asAry()));
144  }
145 
146  // Field lookup is prefix or backwards: ".x term"
147  if( BUF[X]=='.' ) {
148  X++;
149  return new Field(id(),term());
150  }
151 
152  throw unimpl("Unknown syntax");
153  }

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

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

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

◆ toString()

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

Definition at line 92 of file HM8.java.

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

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

Member Data Documentation

◆ BUF

◆ DEBUG_LEAKS

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

Definition at line 26 of file HM8.java.

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

◆ ID

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

Definition at line 154 of file HM8.java.

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

◆ PRIMS

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

◆ X


The documentation for this class was generated from the following file:
com.cliffc.aa.HM.HM8.skipWS
static byte skipWS()
Definition: HM8.java:178
com.cliffc.aa.HM.HM8.require
static< T > T require(char c, T t)
Definition: HM8.java:186
com.cliffc.aa.HM.HM8.id
static String id()
Definition: HM8.java:155
com.cliffc.aa.util.Ary.push
E push(E e)
Add element in amortized constant time.
Definition: Ary.java:58
com.cliffc.aa.HM.HM8.parse
static Syntax parse(String s)
Definition: HM8.java:93
com.cliffc.aa.HM.HM8.isAlpha0
static boolean isAlpha0(byte c)
Definition: HM8.java:184
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.BitsAlias
Definition: BitsAlias.java:8
com.cliffc.aa.HM.HM8.X
static int X
Definition: HM8.java:90
com.cliffc.aa.type.TypeInt.con
static TypeInt con(long con)
Definition: TypeInt.java:37
com.cliffc.aa.HM.HM8.number
static Syntax number()
Definition: HM8.java:163
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.type.TypeFlt.con
static Type con(double con)
Definition: TypeFlt.java:36
com.cliffc.aa.HM.HM8.term
static Syntax term()
Definition: HM8.java:100
com.cliffc.aa.HM.HM8.DEBUG_LEAKS
static boolean DEBUG_LEAKS
Definition: HM8.java:26
com.cliffc.aa.HM.HM8.isAlpha1
static boolean isAlpha1(byte c)
Definition: HM8.java:185
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.HM8.isWS
static boolean isWS(byte c)
Definition: HM8.java:182
com.cliffc.aa.util.SB.p
SB p(String s)
Definition: SB.java:13
com.cliffc.aa.HM.HM8.ID
static final SB ID
Definition: HM8.java:154
com.cliffc.aa.HM.HM8.isDigit
static boolean isDigit(byte c)
Definition: HM8.java:183
com.cliffc.aa.HM.HM8.string
static Syntax string()
Definition: HM8.java:173
com.cliffc.aa.type.TypeMemPtr.STRPTR
static final TypeMemPtr STRPTR
Definition: TypeMemPtr.java:97
com.cliffc.aa.HM.HM8.PRIMS
static final HashMap< String, T2 > PRIMS
Definition: HM8.java:25
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.type.TypeMemPtr
Definition: TypeMemPtr.java:14
com.cliffc.aa.type.TypeFlt.FLT64
static final TypeFlt FLT64
Definition: TypeFlt.java:38
com.cliffc.aa.type.BitsAlias.reset_to_init0
static void reset_to_init0()
Definition: BitsAlias.java:64
com.cliffc.aa.HM.HM8.BUF
static byte[] BUF
Definition: HM8.java:91