Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| Comment: | intrinsic functions |
|---|---|
| Downloads: | Tarball | ZIP archive |
| Timelines: | family | ancestors | trunk |
| Files: | files | file ages | folders |
| SHA3-256: |
906aaa2d5e8713a8816668e48ed26cc1 |
| User & Date: | athaudia 2017-08-28 21:25:03.432 |
Context
|
2017-08-28
| ||
| 21:25 | intrinsic functions Leaf check-in: 906aaa2d5e user: athaudia tags: trunk | |
| 20:07 | annotation check-in: 2d65274902 user: athaudia tags: trunk | |
Changes
Changes to src/AnnotateStep.cs.
| ︙ | ︙ | |||
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
}
public class Function {
public string name;
public Type type;
public List<Type> parameters = new List<Type>();
public Block block;
}
public class Block {
public Scope scope;
public List<Statement> statements = new List<Statement>();
}
| > | 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
}
public class Function {
public string name;
public Type type;
public List<Type> parameters = new List<Type>();
public Block block;
public bool intrinsic;
}
public class Block {
public Scope scope;
public List<Statement> statements = new List<Statement>();
}
|
| ︙ | ︙ | |||
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
t.Add(new Type{name = "i64", size = 64, builtin = true});
t.Add(new Type{name = "u8", size = 8, builtin = true});
t.Add(new Type{name = "u16", size = 16, builtin = true});
t.Add(new Type{name = "u32", size = 32, builtin = true});
t.Add(new Type{name = "u64", size = 64, builtin = true});
t.Add(new Type{name = "string", size = -1, builtin = true});
t.Add(new Type{name = "bool", size = 1, builtin = true});
}
static Type
GetType(Top top, string name) {
var t = top.types.First(item => item.name == name);
if(t == null)
throw new ArgumentOutOfRangeException("Unknown type: " + name);
return t;
}
static Function
GetFunction(Top top, string name, List<Type> pars) {
foreach(var fun in top.functions) {
if(fun.name == name && fun.parameters.Count == pars.Count) {
| > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | > | | 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
t.Add(new Type{name = "i64", size = 64, builtin = true});
t.Add(new Type{name = "u8", size = 8, builtin = true});
t.Add(new Type{name = "u16", size = 16, builtin = true});
t.Add(new Type{name = "u32", size = 32, builtin = true});
t.Add(new Type{name = "u64", size = 64, builtin = true});
t.Add(new Type{name = "string", size = -1, builtin = true});
t.Add(new Type{name = "bool", size = 1, builtin = true});
var cmp_types = new[]{"i8","i16","i32","i64","u8","u16","u32","u64"};
var eq_types = cmp_types.Concat(new[]{"string", "bool"}).ToArray();
var cmp_funs = new[]{"cmp_lt", "cmp_leq", "cmp_gt", "cmp_geq"};
var num_funs = new[]{"add", "sub", "mul", "div"};
var eq_funs = new[]{"cmp_eq", "cmp_neq"};
var bool_ty = GetType(top, "bool");
foreach(var cmp in cmp_types) {
var ty = GetType(top, cmp);
foreach(var fun in cmp_funs) {
var f = new Function{name=fun, type=bool_ty, intrinsic=true};
f.parameters.Add(ty);
f.parameters.Add(ty);
top.functions.Add(f);
}
foreach(var fun in num_funs) {
var f = new Function{name=fun, type=ty, intrinsic=true};
f.parameters.Add(ty);
f.parameters.Add(ty);
top.functions.Add(f);
}
}
foreach(var eq in eq_types) {
var ty = GetType(top, eq);
foreach(var fun in eq_funs) {
var f = new Function{name=fun, type=bool_ty, intrinsic=true};
f.parameters.Add(ty);
f.parameters.Add(ty);
top.functions.Add(f);
}
}
}
static Type
GetType(Top top, string name) {
var t = top.types.First(item => item.name == name);
if(t == null)
throw new ArgumentOutOfRangeException("Unknown type: " + name);
return t;
}
static Function
GetFunction(Top top, string name, List<Type> pars) {
foreach(var fun in top.functions) {
if(fun.name == name && fun.parameters.Count == pars.Count) {
var ok = true;
for(var i = 0; i < pars.Count && ok; ++i) {
if(fun.parameters[i] != pars[i])
ok = false;
}
if(ok)
return fun;
}
}
return null;
}
static Variable
GetVariable(Scope scope, string name) {
|
| ︙ | ︙ | |||
161 162 163 164 165 166 167 |
Annotate(ParseStep.Top ptop) {
var top = new Top();
InitIntrinsics(top);
var dict = new Dictionary<ParseStep.Function, Function>();
//pre annotate functions(without their body)
foreach(var pf in ptop.functions) {
var scope = new Scope();
| | | 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
Annotate(ParseStep.Top ptop) {
var top = new Top();
InitIntrinsics(top);
var dict = new Dictionary<ParseStep.Function, Function>();
//pre annotate functions(without their body)
foreach(var pf in ptop.functions) {
var scope = new Scope();
var f = new Function {name = pf.name, intrinsic = false};
f.type = GetType(top, pf.type.name);
foreach(var pfp in pf.parameters) {
var t = GetType(top, pfp.type.name);
var v = new Variable(pfp.name, t, true);
scope.variables.Add(v);
f.parameters.Add(t);
}
|
| ︙ | ︙ | |||
250 251 252 253 254 255 256 257 |
}
block.scope.variables.Add(va);
var st = new StatementAssignment();
st.left = new ExpressionVariableUse(){variable=va};
st.right = exp;
stmts.Add(st);
break;
default:
| > > > > > > > | | 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
}
block.scope.variables.Add(va);
var st = new StatementAssignment();
st.left = new ExpressionVariableUse(){variable=va};
st.right = exp;
stmts.Add(st);
break;
case ParseStep.StatementExpression pexpr:
var ex = new StatementExpression();
ex.expression = Annotate(pexpr.expression, top, block.scope);
stmts.Add(ex);
break;
default:
var emsg = "unknown statement type: ";
emsg += ps.GetType().Name;
throw new InvalidOperationException(emsg);
}
}
}
public static ConditionalBlock
Annotate(ParseStep.ConditionalBlock pblock, Scope parent_scope,
Function fun, Top top)
|
| ︙ | ︙ | |||
287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
if(fun.function == null) {
var m = "can't find function "+pfuncall.function+"(";
foreach(var t in types)
m += t.name+", ";
m += ")";
throw new InvalidOperationException(m);
}
return fun;
case ParseStep.ExpressionMemberAccess pmember:
var member = new ExpressionMemberAccess();
throw new InvalidOperationException("nope");
//todo: can't do yet, need to implement those types first :P
case ParseStep.ExpressionVariableUse pvaruse:
var varuse = new ExpressionVariableUse();
| > | 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
if(fun.function == null) {
var m = "can't find function "+pfuncall.function+"(";
foreach(var t in types)
m += t.name+", ";
m += ")";
throw new InvalidOperationException(m);
}
fun.type = fun.function.type;
return fun;
case ParseStep.ExpressionMemberAccess pmember:
var member = new ExpressionMemberAccess();
throw new InvalidOperationException("nope");
//todo: can't do yet, need to implement those types first :P
case ParseStep.ExpressionVariableUse pvaruse:
var varuse = new ExpressionVariableUse();
|
| ︙ | ︙ |
Changes to src/test.rcs.
1 2 3 4 5 6 7 8 9 10 11 12 | fun factorial(n:i32):i32 var result = 1 var i = 1 while i <= n result = result * i i = i + 1 end return result end fun main():i32 var i = 0 | < < < | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | fun factorial(n:i32):i32 var result = 1 var i = 1 while i <= n result = result * i i = i + 1 end return result end fun main():i32 var i = 0 return factorial(3*3) end |