aa
com.cliffc.aa.HM.HM7 Class Reference
Collaboration diagram for com.cliffc.aa.HM.HM7:
[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 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 HM7.java.

Member Function Documentation

◆ hm()

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

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

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

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

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

◆ id()

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

Definition at line 158 of file HM7.java.

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

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

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

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

◆ isAlpha0()

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

Definition at line 187 of file HM7.java.

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

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

Here is the caller graph for this function:

◆ isAlpha1()

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

Definition at line 188 of file HM7.java.

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

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

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

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

◆ isDigit()

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

Definition at line 186 of file HM7.java.

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

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

Here is the caller graph for this function:

◆ isWS()

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

Definition at line 185 of file HM7.java.

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

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

Here is the caller graph for this function:

◆ number()

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

Definition at line 166 of file HM7.java.

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

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

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

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

◆ parse()

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

Definition at line 96 of file HM7.java.

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

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

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

Definition at line 189 of file HM7.java.

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

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

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

Definition at line 190 of file HM7.java.

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

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

Here is the call graph for this function:

◆ reset()

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

Definition at line 89 of file HM7.java.

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

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

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

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

◆ skipWS()

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

Definition at line 181 of file HM7.java.

181  {
182  while( X<BUF.length && isWS(BUF[X]) ) X++;
183  return X==BUF.length ? -1 : BUF[X];
184  }

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

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

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

◆ string()

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

Definition at line 176 of file HM7.java.

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

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

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

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

◆ term()

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

Definition at line 103 of file HM7.java.

103  {
104  if( skipWS()==-1 ) return null;
105  if( isDigit(BUF[X]) ) return number();
106  if( BUF[X]=='"' ) return string();
107  if( BUF[X]=='(' ) { // Parse an Apply
108  X++; // Skip paren
109  Syntax fun = term();
110  Ary<Syntax> ARGS = new Ary<>(new Syntax[1],0);
111  while( skipWS()!= ')' && X<BUF.length ) ARGS.push(term());
112  return new Apply(fun,require(')',ARGS.asAry()));
113  }
114  if( BUF[X]=='{' ) { // Lambda of 1 or 2 args
115  X++; // Skip paren
116  Ary<String> args = new Ary<>(new String[1],0);
117  while( skipWS()!='-' ) args.push(id());
118  require("->");
119  Syntax body = require('}',term());
120  return new Lambda(body,args.asAry());
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  // Structure
133  if( BUF[X]=='@' ) {
134  X++;
135  require('{',null);
136  Ary<String> ids = new Ary<>(String.class);
137  Ary<Syntax> flds = new Ary<>(Syntax.class);
138  while( skipWS()!='}' && X < BUF.length ) {
139  String id = require('=',id());
140  Syntax fld = term();
141  if( fld==null ) throw unimpl("Missing term for field "+id);
142  ids .push( id);
143  flds.push(fld);
144  if( skipWS()==',' ) X++;
145  }
146  return require('}',new Struct(ids.asAry(),flds.asAry()));
147  }
148 
149  // Field lookup is prefix or backwards: ".x term"
150  if( BUF[X]=='.' ) {
151  X++;
152  return new Field(id(),term());
153  }
154 
155  throw unimpl("Unknown syntax");
156  }

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

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

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

◆ toString()

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

Definition at line 95 of file HM7.java.

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

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

Member Data Documentation

◆ BUF

◆ DEBUG_LEAKS

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

Definition at line 25 of file HM7.java.

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

◆ ID

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

Definition at line 157 of file HM7.java.

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

◆ PRIMS

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

◆ X


The documentation for this class was generated from the following file:
com.cliffc.aa.HM.HM7.term
static Syntax term()
Definition: HM7.java:103
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.HM.HM7.id
static String id()
Definition: HM7.java:158
com.cliffc.aa.type.TypeInt.con
static TypeInt con(long con)
Definition: TypeInt.java:37
com.cliffc.aa.HM.HM7.ID
static final SB ID
Definition: HM7.java:157
com.cliffc.aa.HM.HM7.string
static Syntax string()
Definition: HM7.java:176
com.cliffc.aa.HM.HM7.PRIMS
static final HashMap< String, T2 > PRIMS
Definition: HM7.java:24
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.HM7.isWS
static boolean isWS(byte c)
Definition: HM7.java:185
com.cliffc.aa.type.TypeFlt.con
static Type con(double con)
Definition: TypeFlt.java:36
com.cliffc.aa.HM.HM7.isDigit
static boolean isDigit(byte c)
Definition: HM7.java:186
com.cliffc.aa.HM.HM7.skipWS
static byte skipWS()
Definition: HM7.java:181
com.cliffc.aa.HM.HM7.parse
static Syntax parse(String s)
Definition: HM7.java:96
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.HM7.X
static int X
Definition: HM7.java:93
com.cliffc.aa.HM.HM7.number
static Syntax number()
Definition: HM7.java:166
com.cliffc.aa.HM.HM7.BUF
static byte[] BUF
Definition: HM7.java:94
com.cliffc.aa.HM.HM7.isAlpha0
static boolean isAlpha0(byte c)
Definition: HM7.java:187
com.cliffc.aa.util.SB.p
SB p(String s)
Definition: SB.java:13
com.cliffc.aa.type.TypeMemPtr.STRPTR
static final TypeMemPtr STRPTR
Definition: TypeMemPtr.java:97
com.cliffc.aa.HM.HM7.DEBUG_LEAKS
static boolean DEBUG_LEAKS
Definition: HM7.java:25
com.cliffc.aa.HM.HM7.isAlpha1
static boolean isAlpha1(byte c)
Definition: HM7.java:188
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.HM.HM7.require
static< T > T require(char c, T t)
Definition: HM7.java:189