void VarNameDecl(model2mbed.TypeDecl type) { string name; model2mbed.Expression arg1 = null, arg2 = null; Ident(out name); model2mbed.VarDecl vardecl = new model2mbed.VarDecl(name, type); decls.Add(vardecl); if (la.kind == 11) { Get(); Expression(out arg1); Expect(12); int low = 0, length = 0; if (!decls.getArrSizeValue(arg1, out low, out length)) { throw new FatalError("Could not calculate array size!"); } vardecl.IsArray = true; vardecl.ArrLow = low; vardecl.ArrLength = length; } if (la.kind == 13) { Get(); Expression(out arg2); vardecl.Expr = arg2; vardecl.HasExpr = true; } }
public bool getVarValue(string varname, out int val) { val = 0; VarDecl res = getVar(varname); if (res == null) { throw new ParseException(String.Format("Value '{0}': unknown!", varname)); } if (res.IsArray) { throw new ParseException(String.Format("Value '{0}': Indexers not yet support on consts!", varname)); } if (res.Type.Type != VarType.Int) { throw new ParseException(String.Format("Value '{0}': Only integer values can be used right now!", varname)); } if (!res.HasExpr) { throw new ParseException(String.Format("Value '{0}': not initialized!", varname)); } return(getExprValue(res.Expr, out val)); }
public string generate(UpdateRule ur) { VarDecl vd = _decls.getVar(ur.VarName); if (vd == null) { throw new Exception(String.Format("Unknown variable {0}!", ur.VarName)); } string vname; if (vd.IsArray) { if (ur.Expr.Type != Expression.ExpType.Func || ur.Expr.Func != Expression.Funcs.ArrayIndex || ur.Expr.First.Type != Expression.ExpType.Var) { throw new Exception(String.Format("Array variable {0} not accessed by index!", vd.Name)); } vd = _decls.getVar(ur.Expr.First.Var); vname = String.Format("{0}[{1}]", _namer(vd), generate(ur.Expr.Second)); } else { vname = _namer(vd); } return(String.Format("{0}.{1} = ({2});", _stateStructName, vname, generate(ur.Expr))); }
private Rule parseUpdateRule(string pname, string rule, Declarations decls) { int len = 1; int equal = rule.IndexOf(":="); if (equal < 0) { equal = rule.IndexOf('='); } else { len = 2; } if (equal < 0) { throw new ParseException(String.Format("Invalid update rule '{0}'!", rule)); } string varname = rule.Substring(0, equal).Trim(); string upexpr = rule.Substring(equal + len).Trim(); VarDecl vd = decls.getVar(varname); if (vd == null) { throw new ParseException(String.Format("Unknown variable {0}!", varname)); } string expr = String.Format("int _UPDATE_{0} = {1};", varname, upexpr); Declarations newdecls = parseDeclarations(expr, decls); Expression e = newdecls.getFirstDecl().Expr; return(new UpdateRule(varname, e)); }
private void FormatSingleVarDeclaration(string typestr, string prefix, VarDecl vd) { string arpostfix = getArraryDecl(vd); string name = setUniqueName(vd, String.Format("{0}_{1}_{2}{3}", typestr, prefix, vd.Name, arpostfix)); app(" {0}{1} {2};", vd.Type.Const ? "const " : "", typestr, name); }
private void FormatChannel(string template, VarDecl v, string name, string postfix) { if (_isSource) { app("SYNCHRONIZATION_CHANNEL_STATE {0}{1}_STATE; // {2}", name, postfix, v.NiceName()); app(); app("const SYNCHRONIZATION_CHANNEL_DATA {0}{1}_DATA = // {2}", name, postfix, v.NiceName()); app(" {{"); app(" DBGSTR(\"{0}[{1}]\"), // name", v.NiceName(), postfix.TrimStart('_')); app(" {0}, // urgent", formatBool(v.Type.Urgent)); app(" {0}, // broadcast", formatBool(v.Type.Broadcast)); app(" &{0}, // state", String.Format("{0}{1}_STATE", name, postfix)); app(" }};"); app(); app("SYNCHRONIZATION_CHANNEL {0}{1} = // {2}", name, postfix, v.NiceName()); app(" {{"); app(" &{0}{1}_DATA, // data", name, postfix); app(" NULL, // fired"); app(" NULL, // context"); app(" }};"); app(); } else { app("extern ModelImpl::SYNCHRONIZATION_CHANNEL {0}{1}; // {2}", name, postfix, v.NiceName()); } }
private void FormatChannel(string template, VarDecl v) { string name = getVarDeclName(v, template); if (v.IsArray) { for (int i = v.ArrLow; i < (v.ArrLow + v.ArrLength); ++i) { FormatChannel(template, v, name, String.Format("_{0}", i)); } if (_isSource) { app(); app("ModelImpl::SYNCHRONIZATION_CHANNEL* const {0}_ARRAY[] = ", name); app(" {{"); for (int i = v.ArrLow; i < (v.ArrLow + v.ArrLength); ++i) { app(" &{0}{1},", name, String.Format("_{0}", i)); } app(" NULL"); app(" }};"); app(); } } else { FormatChannel(template, v, name, ""); } }
public TypeDecl GetType(Declarations symtab) { switch (Type) { case ExpType.Const: return(new TypeDecl() { Type = VarType.Int }); case ExpType.Var: VarDecl v = symtab.getVar(Var); if (v == null) { throw new ParseException(String.Format("Unknown variable {0}", Var)); } return(v.Type); case ExpType.Func: // this should be improved later return(new TypeDecl() { Type = VarType.Int }); default: throw new ParseException("Internal error 1"); } }
private static string getArraryDecl(VarDecl vd) { string postfix = ""; if (vd.IsArray) { postfix = String.Format("[{0}]", vd.ArrLength); } return(postfix); }
private string getVarDeclName(VarDecl v, string template) { string name; if (_names.TryGetValue(v, out name)) { return(name); } return(setUniqueName(v, String.Format("{0}_CHANNEL_VAR_{1}", template, v.Name))); }
public VarDecl getVar(string name) { VarDecl vdef = _vdefs.SingleOrDefault(var => var.Name == name); if (vdef != null) { return(vdef); } if (Parent != null) { return(Parent.getVar(name)); } return(null); }
public VarWithScope getVarWithScope(string name) { VarDecl vdef = _vdefs.SingleOrDefault(var => var.Name == name); if (vdef != null) { return new VarWithScope() { var = vdef, decl = this } } ; if (Parent != null) { return(Parent.getVarWithScope(name)); } return(null); }
private void FormatSingleVarInitialization(VarDecl vd, Declarations decl) { string name = getUniqueName(vd); if (!vd.HasExpr) { return; } int val; if (!decl.getExprValue(vd.Expr, out val)) { throw new Exception(String.Format("Cannot use non const variable!")); } app(" {0}({1}),", name, val); }
public void Add(VarDecl vdecl) { verifyCleanName(vdecl.Name); _vdefs.Add(vdecl); }
public void removeVar(VarDecl vd) { _vdefs.Remove(vd); }