public ScriptVariable(System.IO.BinaryReader reader, int size) { index = reader.ReadByte(); type = (ScriptVariableType)reader.ReadByte(); function = (ScriptFunction)reader.ReadInt16(); compareOp = (CompareOp)reader.ReadByte(); variable = reader.ReadString(size - 5); // Do a switch here to determine the variable to save looking it up each frame switch (type) { case ScriptVariableType.Global: global = Record.GetRecord <Global>(variable); break; case ScriptVariableType.Item: item = Record.GetRecord <ItemRecord>(variable); break; case ScriptVariableType.NotClass: characterClass = Record.GetRecord <ClassRecord>(variable); break; case ScriptVariableType.NotFaction: faction = Record.GetRecord <Faction>(variable); break; case ScriptVariableType.NotRace: race = Record.GetRecord <Race>(variable); break; } }
public static ScriptVariableType GetAcceptableTypes(ScriptVariableType requiredResultType) { // Explicit cast exists from any to any type return(ScriptVariableType.AnyValue); /* * switch( requiredResultType ) { * case ScriptVariableType.Bool: return ScriptVariableType.Bool; * case ScriptVariableType.Byte: return ScriptVariableType.Byte | ScriptVariableType.SByte | ScriptVariableType.Short | ScriptVariableType.UShort | ScriptVariableType.Int | ScriptVariableType.UInt | ScriptVariableType.Long | ScriptVariableType.ULong | ScriptVariableType.Float | ScriptVariableType.Double | ScriptVariableType.Decimal | ScriptVariableType.Char | ScriptVariableType.String; * case ScriptVariableType.SByte: return ScriptVariableType.Byte | ScriptVariableType.SByte | ScriptVariableType.Short | ScriptVariableType.UShort | ScriptVariableType.Int | ScriptVariableType.UInt | ScriptVariableType.Long | ScriptVariableType.ULong | ScriptVariableType.Float | ScriptVariableType.Double | ScriptVariableType.Decimal | ScriptVariableType.Char | ScriptVariableType.String; * case ScriptVariableType.Short: return ScriptVariableType.Byte | ScriptVariableType.SByte | ScriptVariableType.Short | ScriptVariableType.UShort | ScriptVariableType.Int | ScriptVariableType.UInt | ScriptVariableType.Long | ScriptVariableType.ULong | ScriptVariableType.Float | ScriptVariableType.Double | ScriptVariableType.Decimal | ScriptVariableType.Char | ScriptVariableType.String; * case ScriptVariableType.UShort: return ScriptVariableType.Byte | ScriptVariableType.SByte | ScriptVariableType.Short | ScriptVariableType.UShort | ScriptVariableType.Int | ScriptVariableType.UInt | ScriptVariableType.Long | ScriptVariableType.ULong | ScriptVariableType.Float | ScriptVariableType.Double | ScriptVariableType.Decimal | ScriptVariableType.Char | ScriptVariableType.String; * case ScriptVariableType.Int: return ScriptVariableType.Byte | ScriptVariableType.SByte | ScriptVariableType.Short | ScriptVariableType.UShort | ScriptVariableType.Int | ScriptVariableType.UInt | ScriptVariableType.Long | ScriptVariableType.ULong | ScriptVariableType.Float | ScriptVariableType.Double | ScriptVariableType.Decimal | ScriptVariableType.Char | ScriptVariableType.String; * case ScriptVariableType.UInt: return ScriptVariableType.Byte | ScriptVariableType.SByte | ScriptVariableType.Short | ScriptVariableType.UShort | ScriptVariableType.Int | ScriptVariableType.UInt | ScriptVariableType.Long | ScriptVariableType.ULong | ScriptVariableType.Float | ScriptVariableType.Double | ScriptVariableType.Decimal | ScriptVariableType.Char | ScriptVariableType.String; * case ScriptVariableType.Long: return ScriptVariableType.Byte | ScriptVariableType.SByte | ScriptVariableType.Short | ScriptVariableType.UShort | ScriptVariableType.Int | ScriptVariableType.UInt | ScriptVariableType.Long | ScriptVariableType.ULong | ScriptVariableType.Float | ScriptVariableType.Double | ScriptVariableType.Decimal | ScriptVariableType.Char | ScriptVariableType.String; * case ScriptVariableType.ULong: return ScriptVariableType.Byte | ScriptVariableType.SByte | ScriptVariableType.Short | ScriptVariableType.UShort | ScriptVariableType.Int | ScriptVariableType.UInt | ScriptVariableType.Long | ScriptVariableType.ULong | ScriptVariableType.Float | ScriptVariableType.Double | ScriptVariableType.Decimal | ScriptVariableType.Char | ScriptVariableType.String; * case ScriptVariableType.Float: return ScriptVariableType.Byte | ScriptVariableType.SByte | ScriptVariableType.Short | ScriptVariableType.UShort | ScriptVariableType.Int | ScriptVariableType.UInt | ScriptVariableType.Long | ScriptVariableType.ULong | ScriptVariableType.Float | ScriptVariableType.Double | ScriptVariableType.Decimal | ScriptVariableType.Char | ScriptVariableType.String; * case ScriptVariableType.Double: return ScriptVariableType.Byte | ScriptVariableType.SByte | ScriptVariableType.Short | ScriptVariableType.UShort | ScriptVariableType.Int | ScriptVariableType.UInt | ScriptVariableType.Long | ScriptVariableType.ULong | ScriptVariableType.Float | ScriptVariableType.Double | ScriptVariableType.Decimal | ScriptVariableType.Char | ScriptVariableType.String; * case ScriptVariableType.Decimal: return ScriptVariableType.Byte | ScriptVariableType.SByte | ScriptVariableType.Short | ScriptVariableType.UShort | ScriptVariableType.Int | ScriptVariableType.UInt | ScriptVariableType.Long | ScriptVariableType.ULong | ScriptVariableType.Float | ScriptVariableType.Double | ScriptVariableType.Decimal | ScriptVariableType.Char | ScriptVariableType.String; * case ScriptVariableType.Char: return ScriptVariableType.Byte | ScriptVariableType.SByte | ScriptVariableType.Short | ScriptVariableType.UShort | ScriptVariableType.Int | ScriptVariableType.UInt | ScriptVariableType.Long | ScriptVariableType.ULong | ScriptVariableType.Float | ScriptVariableType.Double | ScriptVariableType.Decimal | ScriptVariableType.Char | ScriptVariableType.String; * case ScriptVariableType.String: return ScriptVariableType.Bool | ScriptVariableType.Byte | ScriptVariableType.SByte | ScriptVariableType.Short | ScriptVariableType.UShort | ScriptVariableType.Int | ScriptVariableType.UInt | ScriptVariableType.Long | ScriptVariableType.ULong | ScriptVariableType.Float | ScriptVariableType.Double | ScriptVariableType.Decimal | ScriptVariableType.Char | ScriptVariableType.String; * } * throw new UserFriendlyException(String.Format("{0} cannot decide on acceptable types for {1}", this.GetType().Name, requiredResultType), "An error in scripting engine was encountered"); */ }
public static Type GetSystemType(this ScriptVariableType type) { switch (type) { case ScriptVariableType.Bool: return(typeof(bool)); case ScriptVariableType.Byte: return(typeof(byte)); case ScriptVariableType.SByte: return(typeof(sbyte)); case ScriptVariableType.Char: return(typeof(char)); case ScriptVariableType.UShort: return(typeof(ushort)); case ScriptVariableType.Short: return(typeof(short)); case ScriptVariableType.UInt: return(typeof(uint)); case ScriptVariableType.Int: return(typeof(int)); case ScriptVariableType.ULong: return(typeof(ulong)); case ScriptVariableType.Long: return(typeof(long)); case ScriptVariableType.Float: return(typeof(float)); case ScriptVariableType.Double: return(typeof(double)); case ScriptVariableType.Decimal: return(typeof(decimal)); case ScriptVariableType.String: return(typeof(string)); } throw new UserFriendlyException(String.Format("There is no sample value for variable type '{0}'", type), "There was an error in scripting engine"); }
public override ScriptVariableType Process(ScriptParser parser, int level) { base.Process(parser, level); bool hadErrors = false; try { LType = LValue.Process(parser, level + 1); } catch (ScriptCriticalErrorException) { hadErrors = true; } try { RType = RValue.Process(parser, level + 1); } catch (ScriptCriticalErrorException) { hadErrors = true; } if (hadErrors) { throw new ScriptCriticalErrorException(); // we are not going to try the operator because one of nodes had an error } dynamic a, b, c; a = LType.GetSampleValue(); b = RType.GetSampleValue(); try { c = ApplyOperator(a, b); ResultType = ScriptVariableTypeExtension.GetScriptVariableType(c); } catch (UserFriendlyException) { throw; } catch { parser.Errors.Add(new ErrorInfo(ErrorLevel.CriticalError, ErrorCode.OperatorCannotBeApplied2, this, LType, RType)); throw new ScriptCriticalErrorException(); } return(ResultType); }
public static dynamic GetDefaultValue(this ScriptVariableType type) { switch (type) { case ScriptVariableType.Bool: return(false); case ScriptVariableType.Byte: return((byte)0); case ScriptVariableType.SByte: return((sbyte)0); case ScriptVariableType.Char: return((char)0); case ScriptVariableType.UShort: return((ushort)0); case ScriptVariableType.Short: return((short)0); case ScriptVariableType.UInt: return((uint)0); case ScriptVariableType.Int: return((int)0); case ScriptVariableType.ULong: return((ulong)0); case ScriptVariableType.Long: return((long)0); case ScriptVariableType.Float: return((float)0); case ScriptVariableType.Double: return((double)0); case ScriptVariableType.Decimal: return((decimal)0); case ScriptVariableType.String: return(null); // case ScriptVariableType.Undefined: throw new ScriptCriticalErrorException(); } throw new UserFriendlyException(String.Format("There is no sample value for variable type '{0}'", type), "There was an error in scripting engine"); }
public static ScriptVariable ConvertTo(this ScriptVariable self, ScriptVariableType type) { if (self.IsNull()) { return(null); } if (self.Type == type) { return(self); } switch (type) { case ScriptVariableType.Boolean: return(new BooleanVariable(self.ToBoolean())); case ScriptVariableType.List: return(new ListVariable(self.ToList())); case ScriptVariableType.Number: return(new NumberVariable(self.ToNumber())); case ScriptVariableType.String: return(new StringVariable(self.ToString())); //case ScriptVariableType.Object: //return new ObjectVariable(self.ToObject()); } return(null); }
public void AddVariable(Guid id, ScriptVariableType variableType, bool value, string name) { FlagDictionary.Add(id, value); VariableNamesById.Add(id, name); VariableTypeById.Add(id, variableType); VariableIdByName.Add(name, id); }
public static IExecutionTreeNode ImplicitCast(ScriptParser parser, int level, ScriptVariableType requiredType, IExecutionTreeNode node) { ScriptVariableType acceptableTypes = ScriptVariableType.Undefined; // implicit cast is allowed only in loseless cases (except floating point types) switch (requiredType) { case ScriptVariableType.Short: acceptableTypes = ScriptVariableType.Short | ScriptVariableType.Byte | ScriptVariableType.SByte; break; case ScriptVariableType.UShort: acceptableTypes = ScriptVariableType.UShort | ScriptVariableType.Byte; break; case ScriptVariableType.Int: acceptableTypes = ScriptVariableType.Int | ScriptVariableType.UShort | ScriptVariableType.Short | ScriptVariableType.Byte | ScriptVariableType.SByte; break; case ScriptVariableType.UInt: acceptableTypes = ScriptVariableType.UInt | ScriptVariableType.UShort | ScriptVariableType.Byte; break; case ScriptVariableType.Long: acceptableTypes = ScriptVariableType.Long | ScriptVariableType.UInt | ScriptVariableType.Int | ScriptVariableType.UShort | ScriptVariableType.Short | ScriptVariableType.Byte | ScriptVariableType.SByte; break; case ScriptVariableType.ULong: acceptableTypes = ScriptVariableType.ULong | ScriptVariableType.UInt | ScriptVariableType.UShort | ScriptVariableType.Byte; break; case ScriptVariableType.Float: acceptableTypes = ScriptVariableType.Float | ScriptVariableType.UInt | ScriptVariableType.Int | ScriptVariableType.UShort | ScriptVariableType.Short | ScriptVariableType.Byte | ScriptVariableType.SByte; break; case ScriptVariableType.Double: acceptableTypes = ScriptVariableType.Double | ScriptVariableType.Float | ScriptVariableType.ULong | ScriptVariableType.Long | ScriptVariableType.UInt | ScriptVariableType.Int | ScriptVariableType.UShort | ScriptVariableType.Short | ScriptVariableType.Byte | ScriptVariableType.SByte; break; case ScriptVariableType.Decimal: acceptableTypes = ScriptVariableType.Decimal | ScriptVariableType.Double | ScriptVariableType.Float | ScriptVariableType.ULong | ScriptVariableType.Long | ScriptVariableType.UInt | ScriptVariableType.Int | ScriptVariableType.UShort | ScriptVariableType.Short | ScriptVariableType.Byte | ScriptVariableType.SByte; break; } acceptableTypes = acceptableTypes | requiredType; ScriptVariableType type = node.Process(parser, level); if (type == requiredType) { return(node); } if ((type & acceptableTypes) == ScriptVariableType.Undefined) { parser.Errors.Add(new ErrorInfo(ErrorLevel.Error, ErrorCode.NoImplicitConversion, node, type, requiredType)); return(node); // assume when the error is fixed no implicit cast will be required } TypeCastExecutionNode typeCast = new TypeCastExecutionNode(node.Parent, node.Line, node.Character); node.Parent = typeCast; typeCast.CastFromType = type; typeCast.CastToType = requiredType; typeCast.Value = node; typeCast.IsImplicit = true; typeCast.CastToSystemType = typeCast.CastToType.GetSystemType(); if (parser.DebugParsing) { Utils.IndentedOutput(level, "Inserting implicit cast from '{0}' to '{1}' ({2})", typeCast.CastFromType, typeCast.CastToType, typeCast.CastToSystemType); } return(typeCast); }
/// <summary> /// 変数の値と,その値が利用されている回数のペアのディクショナリを返します /// </summary> /// <param name="targetTypes">回数を数える変数の値の型</param> /// <param name="ignoreState">回数を数える対象としない条件</param> /// <param name="ignoreVariables">回数を数えない変数の値</param> /// <returns></returns> public IDictionary <ScriptVariable, int> GetOccuranceOfVariables(ScriptVariableType targetTypes, VariableNamedState ignoreState, ICollection <ScriptVariable> ignoreVariables) { Dictionary <ScriptVariable, int> ret = new Dictionary <ScriptVariable, int>(new VariableValueEqualityComparer()); foreach (var refCall in _histories) { getOccuranceOfVariablesAux(ret, refCall.Params, targetTypes, ignoreState, ignoreVariables); } return(ret); }
public static IExecutionTreeNode ExplicitCast(IExecutionTreeNode node, ScriptVariableType fromType, ScriptVariableType toType) { TypeCastExecutionNode typeCast = new TypeCastExecutionNode(node.Parent, node.Line, node.Character); node.Parent = typeCast; typeCast.CastFromType = fromType; typeCast.CastToType = toType; typeCast.Value = node; return(typeCast); }
public static int ComparePriority(this ScriptVariableType a, ScriptVariableType b) { int p1 = Priorities[a], p2 = Priorities[b]; if (p1 == p2) { return(0); } return((p1 < p2) ? -1 : 1); }
public override ScriptVariableType Process(ScriptParser parser, int level) { base.Process(parser, level); if (ExitValue != null) { // we don't care what will be returned, but prefer int if numeric value is going to be returned ExitValueType = ExitValue.Process(parser, level + 1); } return(ScriptVariableType.Undefined); }
/// <summary> /// 関数の戻り値を用いて引数を変数で置き換えます /// </summary> /// <param name="targetTypes">置き換える対象の変数の値の型</param> /// <param name="resultPrefix">戻り値を受け取る</param> /// <param name="useIndexerOfResult">戻り値のインデクサの値を用いるかどうか</param> /// <param name="ignoreState">置き換えの対象としない条件</param> public void ReplaceByResult(ScriptVariableType targetTypes, string resultPrefix, bool useIndexerOfResult, VariableNamedState ignoreState) { int index = 0; foreach (var refCall in _histories) { refCall.ReplaceByName(_replacements, ignoreState); index++; replaceByResultAux(refCall.Result, _replacements, ConstructResultName(resultPrefix, index), targetTypes); } }
protected virtual bool SetValue(DB.Element element) { if (ScriptVariableType.IsInstanceOfType(element)) { Document = element.Document; DocumentGUID = Document.GetFingerprintGUID(); Value = element.Id; UniqueID = element.UniqueId; return(true); } return(false); }
/// <summary> /// 標準的な引数の置き換えのディクショナリを返します /// </summary> /// <param name="minimumOccurance">置き換え対象とする変数の値の生起回数の条件</param> /// <param name="targetTypes">置き換えの対象とする変数の値の型</param> /// <param name="maxIdentifierLength">置き換える名前のプレフィックス部の最大長</param> /// <returns></returns> public VariableReplaceMap GetDefaultReplacement(int minimumOccurance, ScriptVariableType targetTypes, int maxIdentifierLength) { HashSet <ScriptVariable> replacement = new HashSet <ScriptVariable>(new VariableValueEqualityComparer()); bool replaced = true; while (replaced) { replaced = false; var none = this.GetOccuranceOfVariables(ScriptVariableType.List | targetTypes, VariableNamedState.AnyElementNamed, replacement); var sub = none.Where(p => p.Key.IsTypeOf(targetTypes) && p.Value >= minimumOccurance).ToList(); if (sub.Any()) { int maxOccur = sub.Max(p => p.Value); foreach (var pair in sub.Where(p => p.Value == maxOccur)) { if (replacement.Add(pair.Key)) { replaced = true; } } } } var one = this.GetOccuranceOfVariables(ScriptVariableType.List | targetTypes, VariableNamedState.AllElementsNamed, replacement); var subOne = one.Where(p => p.Key.IsTypeOf(targetTypes) && !p.Key.IsTypeOf(ScriptVariableType.List) && p.Value >= minimumOccurance).ToList(); foreach (var pair in subOne) { if (replacement.Add(pair.Key)) { replaced = true; } } VariableReplaceMap ret = new VariableReplaceMap(); foreach (ScriptVariable v in replacement) { string abbrev = Parse.ScriptParser.ConvertStringIntoIdentifier(v.ToString()); string identifier = abbrev; int index = 0; while (ret.ContainsValue(identifier) || _replacements.ContainsValue(identifier)) { index++; identifier = abbrev + index.ToString(); } ret.AddWithRename(v, identifier, maxIdentifierLength); } return(ret); }
public virtual ScriptVariableType Process(ScriptParser parser, int level) { if (parser.DebugParsing) { Utils.IndentedOutput(level, "Parse: {0}", GetType().Name); } ScriptVariableType type = ScriptVariableType.Undefined; try { type = parser.GetVariable(Value).VarType; } catch { parser.Errors.Add(new ErrorInfo(ErrorLevel.CriticalError, ErrorCode.UndefinedVariable, this, Value)); throw new ScriptCriticalErrorException(); } return(type); }
public override ScriptVariableType Process(ScriptParser parser, int level) { base.Process(parser, level); ValueType = Value.Process(parser, level + 1); dynamic a, b; a = ValueType.GetSampleValue(); try { b = ApplyOperator(ref a); ResultType = ScriptVariableTypeExtension.GetScriptVariableType(b); } catch (UserFriendlyException) { throw; } catch { parser.Errors.Add(new ErrorInfo(ErrorLevel.CriticalError, ErrorCode.OperatorCannotBeApplied1, this, ValueType)); throw new ScriptCriticalErrorException(); } return(ValueType); }
public override ScriptVariableType Process(ScriptParser parser, int level) { base.Process(parser, level); CastFromType = Value.Process(parser, level + 1); CastToSystemType = CastToType.GetSystemType(); dynamic a = CastFromType.GetSampleValue(); try { a = Convert.ChangeType(a, CastToSystemType); } catch { parser.Errors.Add(new ErrorInfo(ErrorLevel.Error, ErrorCode.NoExplicitConversion, this, CastFromType, CastToType)); } if (parser.DebugParsing) { Utils.IndentedOutput(level, "Explicit cast from '{0}' to '{1}' ({2})", CastFromType, CastToType, CastToSystemType); } return(CastToType); }
public override ScriptVariableType Process(ScriptParser parser, int level) { base.Process(parser, level); if (parser.RequiredReturnType != ScriptVariableType.Undefined) { // Within a function if (parser.RequiredReturnType == ScriptVariableType.Void && ReturnValue != null) { parser.Errors.Add(new ErrorInfo(ErrorLevel.Error, ErrorCode.FunctionMustNotReturnAValue, this)); } else if (parser.RequiredReturnType != ScriptVariableType.Void) { if (ReturnValue == null) { parser.Errors.Add(new ErrorInfo(ErrorLevel.Error, ErrorCode.FunctionMustReturnAValue, this)); } else { ReturnValue = TypeCastExecutionNode.ImplicitCast(parser, level + 1, parser.RequiredReturnType, ReturnValue); ReturnValueType = parser.RequiredReturnType; } } IsInGlobalScope = false; } else { // Within the global scope. Anything or nothing may be returned (works same as 'exit'). if (ReturnValue != null) { ReturnValueType = ReturnValue.Process(parser, level + 1); } IsInGlobalScope = true; } return(ScriptVariableType.Undefined); }
private static void ParseHeaderNodeDefNode(ScriptProgram program, ScriptNode root, TokenStream stream) { string variablename = ""; string defaultValue = ""; // To be converted later Guid variableId = Guid.Empty; ScriptVariableType variableType = ScriptVariableType.Unknown; ScriptVariableScope variableScope = ScriptVariableScope.Unknown; var parameter = stream.Pop(); while (parameter.TokenType == SemanticTokenType.NodeParameter) { //var argumentValue = stream.Pop(); if (parameter.TokenValue == "name:") { var value = stream.Pop(); variablename = value.TokenValue; } else if (parameter.TokenValue == "type:") { var value = stream.Pop(); if (value.TokenValue == "flag") { variableType = ScriptVariableType.Flag; } else if (value.TokenValue == "int") { variableType = ScriptVariableType.Integer; } else { throw new Exception("Unsupported variable type:" + value.TokenValue); } } else if (parameter.TokenValue == "scope:") { var value = stream.Pop(); if (value.TokenValue == "global") { variableScope = ScriptVariableScope.Global; } else if (value.TokenValue == "local") { variableScope = ScriptVariableScope.Local; } else { throw new Exception("Unsupported scope:" + value); } } else if (parameter.TokenValue == "default:") { var value = stream.Pop(); defaultValue = value.TokenValue; } else { throw new Exception("Unknown parameter:" + parameter.TokenValue); } var nexttype = stream.GetCurrent().TokenType; if (nexttype != SemanticTokenType.NodeParameter) { break; } parameter = stream.Pop(); } // TODO Look up guid properly Console.WriteLine("TODO Look up guid for variable name"); if (variableId == Guid.Empty) { variableId = Guid.NewGuid(); } switch (variableType) { case ScriptVariableType.Integer: { var value = int.Parse(defaultValue); if (variableScope == ScriptVariableScope.Global) { program.GlobalVariables.AddVariable(variableId, variableType, value, variablename); } else if (variableScope == ScriptVariableScope.Local) { root.LocalVariables.AddVariable(variableId, variableType, value, variablename); } break; } case ScriptVariableType.Flag: { var value = bool.Parse(defaultValue); if (variableScope == ScriptVariableScope.Global) { program.GlobalVariables.AddVariable(variableId, variableType, value, variablename); } else if (variableScope == ScriptVariableScope.Local) { root.LocalVariables.AddVariable(variableId, variableType, value, variablename); } else { throw new Exception("Unsupported Variable"); } break; } default: throw new Exception("Unsupported VariableType:" + variableType); } }
protected virtual int TryParseLiteral(out ScriptToken codeObj) { ScriptLiteralTokenType ctype = ScriptLiteralTokenType.None; ScriptVariableType etype = ScriptVariableType.Undefined; int state = 0, pos = this.Position, len = this.Length; char c; int charCode; // use ushort for char code instead of int? codeObj = null; StringBuilder val = null; StringBuilder charCodeHex = null; while (pos < len && state < 100) { c = this.ScriptText[pos++]; switch (state) { case 0: // the very beginning if (c == '@') { ctype = ScriptLiteralTokenType.String; etype = ScriptVariableType.String; state = 20; break; } goto case 1; case 1: // don't know if it is really a literal if (c == 'n') { ctype = ScriptLiteralTokenType.Null; etype = ScriptVariableType.String; state = 80; break; } if (c == 't') { ctype = ScriptLiteralTokenType.Boolean; etype = ScriptVariableType.Bool; state = 90; break; } if (c == 'f') { ctype = ScriptLiteralTokenType.Boolean; etype = ScriptVariableType.Bool; state = 95; break; } if (c == '\'') { ctype = ScriptLiteralTokenType.Char; etype = ScriptVariableType.Char; state = 10; break; } if (c == '"') { ctype = ScriptLiteralTokenType.String; etype = ScriptVariableType.String; val = new StringBuilder(); state = 30; break; } if (c == '+' && SignInLiteral) { ctype = ScriptLiteralTokenType.Decimal; etype = ScriptVariableType.Int; val = new StringBuilder(); state = 41; break; } // possibly a positive number if (c == '-' && SignInLiteral) { ctype = ScriptLiteralTokenType.Decimal; etype = ScriptVariableType.Int; val = new StringBuilder(); val.Append(c); state = 41; break; } // possibly a negative number if (c == '.') { ctype = ScriptLiteralTokenType.Decimal; etype = ScriptVariableType.Double; val = new StringBuilder(); val.Append(c); state = 43; break; } // possibly starts from a mantissa of a floating point number if (c == '0') { etype = ScriptVariableType.Int; val = new StringBuilder(); val.Append(c); state = 40; break; } if (c >= '1' && c <= '9') { ctype = ScriptLiteralTokenType.Decimal; etype = ScriptVariableType.Int; val = new StringBuilder(); val.Append(c); state = 42; break; } return(0); // parsing a character literal case 10: val = new StringBuilder(); if (c == '\\') { state = 11; break; } val.Append(c); state = 12; break; case 11: if (c == '0') { val.Append((char)0); } else if (c == 't') { val.Append('\t'); } else if (c == 'r') { val.Append('\r'); } else if (c == 'n') { val.Append('\n'); } else if (c == 'u') { charCodeHex = new StringBuilder(); state = 15; break; } else if (c == 'x') { charCodeHex = new StringBuilder(); state = 17; break; } else { val.Append(c); } state = 12; break; case 12: if (c != '\'') { return(0); } state = 100; break; case 15: goto case 18; case 16: goto case 18; case 17: goto case 18; case 18: if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) { charCodeHex.Append(c); if (++state > 18) { state = 12; charCode = (int)Convert.ToUInt32(charCodeHex.ToString(), 16); val.Append((char)charCode); charCodeHex = null; } break; } if (charCodeHex.Length > 0) { charCode = (int)Convert.ToUInt32(charCodeHex.ToString(), 16); val.Append((char)charCode); charCodeHex = null; } state = 12; goto case 12; // parsing a string literal without escape sequences case 20: if (c == '"') { val = new StringBuilder(); state = 21; break; } return(0); case 21: if (c == '"') { state = 22; break; } val.Append(c); break; case 22: // two double quoted in a row result in a doublequote added to a string if (c == '"') { val.Append(c); state = 21; break; } pos--; state = 100; break; // parsing a string literal with escape sequences case 30: if (c == '"') { state = 100; break; } if (c == '\\') { state = 31; break; } if (c == '\r' || c == '\n') { return(0); // in C# newlines are not allowed in strings with escape sequences :/ } val.Append(c); break; case 31: if (c == '0') { val.Append((char)0); } else if (c == 't') { val.Append('\t'); } else if (c == 'r') { val.Append('\r'); } else if (c == 'n') { val.Append('\n'); } else if (c == 'u') { charCodeHex = new StringBuilder(); state = 35; break; } else if (c == 'x') { charCodeHex = new StringBuilder(); state = 37; break; } else { val.Append(c); } state = 30; break; case 35: goto case 38; case 36: goto case 38; case 37: goto case 38; case 38: if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) { charCodeHex.Append(c); if (++state > 38) { state = 30; charCode = (int)Convert.ToUInt32(charCodeHex.ToString(), 16); val.Append((char)charCode); charCodeHex = null; } break; } if (charCodeHex.Length > 0) { charCode = (int)Convert.ToUInt32(charCodeHex.ToString(), 16); val.Append((char)charCode); charCodeHex = null; } state = 30; goto case 30; // parsing a numeric value case 40: // possibly a hex/bin/octal value (starts with 0) if (c == 'x' || c == 'X') { ctype = ScriptLiteralTokenType.Hex; val.Clear(); state = 50; break; } if (c == 'b' || c == 'B') { ctype = ScriptLiteralTokenType.Binary; val.Clear(); state = 60; break; } if (c >= '0' && c <= '7') { ctype = ScriptLiteralTokenType.Octal; val.Clear(); val.Append(c); state = 70; break; } ctype = ScriptLiteralTokenType.Decimal; state = 42; goto case 42; case 41: // starts with - or + if (c == '.') { etype = ScriptVariableType.Double; val.Append(c); state = 43; break; } if (c >= '0' && c <= '9') { val.Append(c); state = 42; break; } return(0); case 42: // starts with a digit if (c == '.') { etype = ScriptVariableType.Double; state = 44; break; } if (c >= '0' && c <= '9') { val.Append(c); break; } if (c == 'F' || c == 'f') { etype = ScriptVariableType.Float; state = 100; break; } if (c == 'D' || c == 'd') { etype = ScriptVariableType.Double; state = 100; break; } if (c == 'L' || c == 'l') { etype = ScriptVariableType.Long; state = 100; break; } if (c == 'M' || c == 'm') { etype = ScriptVariableType.Decimal; state = 100; break; } if (c == 'U' || c == 'u') { etype = ScriptVariableType.UInt; state = 79; break; } pos--; state = 100; break; case 43: // starts with -. or just . if (c >= '0' && c <= '9') { val.Append(c); state = 45; break; } return(0); case 44: // first digit of the mantissa if (c >= '0' && c <= '9') { val.Append('.'); val.Append(c); state = 45; break; } pos -= 2; state = 100; break; case 45: // mantissa (only numbers allowed) if (c >= '0' && c <= '9') { val.Append(c); break; } if (c == 'F' || c == 'f') { etype = ScriptVariableType.Float; state = 100; break; } if (c == 'D' || c == 'd') { etype = ScriptVariableType.Double; state = 100; break; } if (c == 'L' || c == 'l') { etype = ScriptVariableType.Long; state = 100; break; } if (c == 'M' || c == 'm') { etype = ScriptVariableType.Decimal; state = 100; break; } if (c == 'U' || c == 'u') { etype = ScriptVariableType.UInt; state = 79; break; } pos--; state = 100; break; // hex value (0x...) case 50: if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) { val.Append(c); state = 51; break; } ctype = ScriptLiteralTokenType.Decimal; pos -= 2; state = 100; break; case 51: if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) { val.Append(c); break; } if (c == 'L' || c == 'l') { etype = ScriptVariableType.Long; state = 100; break; } if (c == 'U' || c == 'u') { etype = ScriptVariableType.UInt; state = 79; break; } pos--; state = 100; break; // bin value (0b...) case 60: if (c == '0' || c == '1') { val.Append(c); state = 61; break; } ctype = ScriptLiteralTokenType.Decimal; pos -= 2; state = 100; break; case 61: if (c == '0' || c == '1') { val.Append(c); break; } if (c == 'L' || c == 'l') { etype = ScriptVariableType.Long; state = 100; break; } if (c == 'U' || c == 'u') { etype = ScriptVariableType.UInt; state = 79; break; } pos--; state = 100; break; // octal value (0...) case 70: if (c >= '0' && c <= '7') { val.Append(c); break; } if (c == '8' || c == '9') { ctype = ScriptLiteralTokenType.Decimal; val.Append(c); state = 42; break; } if (c == '.') { ctype = ScriptLiteralTokenType.Decimal; state = 44; break; } if (c == 'L' || c == 'l') { etype = ScriptVariableType.Long; state = 100; break; } if (c == 'U' || c == 'u') { etype = ScriptVariableType.UInt; state = 79; break; } pos--; state = 100; break; case 79: // number suffix started with u or U if (c == 'L' || c == 'l') { etype = ScriptVariableType.ULong; state = 100; break; } pos--; state = 100; break; // null case 80: if (c == 'u') { state = 81; break; } return(0); case 81: if (c == 'l') { state = 82; break; } return(0); case 82: if (c == 'l') { val = new StringBuilder(); val.Append("null"); state = 100; break; } return(0); // true case 90: if (c == 'r') { state = 91; break; } return(0); case 91: if (c == 'u') { state = 92; break; } return(0); case 92: if (c == 'e') { val = new StringBuilder(); val.Append("true"); state = 100; break; } return(0); // false case 95: if (c == 'a') { state = 96; break; } return(0); case 96: if (c == 'l') { state = 97; break; } return(0); case 97: if (c == 's') { state = 98; break; } return(0); case 98: if (c == 'e') { val = new StringBuilder(); val.Append("false"); state = 100; break; } return(0); default: return(0); } } if (val == null) { return(0); } codeObj = new ScriptLiteralToken(val.ToString(), ctype, etype, Line, Symbol); return(pos - this.Position); }
/// <summary> /// 値を変数に置き換える部分の,置き換えられる変数の中身も変数で置き換えたものを返します /// </summary> /// <param name="targetTypes"></param> /// <returns></returns> public IList <KeyValuePair <RefScriptVariable, string> > GetRecursivelyRenamedVariables(ScriptVariableType targetTypes) { Dictionary <string, IList <string> > dependencies = new Dictionary <string, IList <string> >(); // 変数一覧を作成 RefVariableByName variables = new RefVariableByName(); foreach (var pair in this) { RefScriptVariable refVar = RefScriptVariable.Create(pair.Key, ""); refVar.Name = pair.Value; variables.Add(refVar); } // 変数の値がリストだったら,各要素を名前付き変数で置き換えられるものは置き換える foreach (var variable in variables) { RefListVariable list = variable as RefListVariable; if (list != null) { foreach (var elem in list.List) { IList <string> replacers = elem.ReplaceByName(this, VariableNamedState.None); // 変換された変数から,変換に利用した変数を求められるようにする foreach (string replacer in replacers) { if (!dependencies.ContainsKey(variable.Name)) { dependencies[variable.Name] = new List <string>(); } dependencies[variable.Name].Add(replacer); } } } } HashSet <RefScriptVariable> used = new HashSet <RefScriptVariable>(); Stack <RefScriptVariable> subVariables = new Stack <RefScriptVariable>(variables); List <KeyValuePair <RefScriptVariable, string> > ret = new List <KeyValuePair <RefScriptVariable, string> >(); while (subVariables.Count > 0) { // 確認対象 RefScriptVariable targetVar = subVariables.Peek(); if (!used.Contains(targetVar) && targetVar.Name != null) { string name = targetVar.Name; // 対象の変数の部分または要素が他の変数で置き換えられているときには, // その置き換える変数の方を先に確認する IList <string> dependNames; if (dependencies.TryGetValue(name, out dependNames)) { bool dependExists = false; foreach (string dependName in dependNames) { RefScriptVariable dependVar = variables[dependName]; if (dependVar != null && !used.Contains(dependVar)) { subVariables.Push(dependVar); dependExists = true; } } if (dependExists) { continue; } } // targetVar.Name = null; ret.Add(new KeyValuePair <RefScriptVariable, string>(targetVar, name)); } used.Add(targetVar); subVariables.Pop(); continue; } return(ret); }
private void replaceByResultAux(ScriptVariable resultElem, VariableReplaceMap map, string resultName, ScriptVariableType targetTypes) { if (resultElem != null && (resultElem.Type & targetTypes) != 0) { map[resultElem] = resultName; if (resultElem.Type == ScriptVariableType.List) { int index = 0; foreach (ScriptVariable elem in resultElem.ToList()) { replaceByResultAux(elem, map, string.Format("{0}[{1}]", resultName, index), targetTypes); index++; } } } }
public void getOccuranceOfVariablesAux(IDictionary <ScriptVariable, int> map, IList <RefScriptVariable> variables, ScriptVariableType targetTypes, VariableNamedState ignoreState, ICollection <ScriptVariable> ignoreVariables) { foreach (var variable in variables) { if (variable.SatisfiesNamedState(ignoreState)) { continue; } if (variable.Value == null) { continue; } if (!variable.Value.IsTypeOf(targetTypes)) { continue; } if (!ignoreVariables.Contains(variable.Value)) { if (!map.ContainsKey(variable.Value)) { map[variable.Value] = 0; } map[variable.Value]++; } RefListVariable list = variable as RefListVariable; RefConcatStringVariable concat = variable as RefConcatStringVariable; if (list != null) { getOccuranceOfVariablesAux(map, list.List, targetTypes, ignoreState, ignoreVariables); } else if (concat != null) { if (concat.Strings.Count == 1 && concat.Strings[0].Word == concat.Value.ToString()) { continue; } foreach (var c in concat.Strings) { if (c.SatisfiesNamedState(ignoreState)) { continue; } StringVariable strVar = new StringVariable(c.Word); if (!ignoreVariables.Contains(strVar)) { if (!map.ContainsKey(strVar)) { map[strVar] = 0; } map[strVar]++; } } } } }
public ScriptVariableDefinition(ScriptVariableType varType, ScriptIdentifierToken name) { VarType = varType; Name = name; }
public bool IsTypeOf(ScriptVariableType types) { return((types & this.Type) != 0); }
/// <summary> /// 型を指定するコンストラクタ /// </summary> /// <param name="type">変数の型</param> protected ScriptVariable(ScriptVariableType type) { _type = type; }
public ScriptLiteralToken(string val, ScriptLiteralTokenType type, ScriptVariableType exactType, int line, int character) : base(val, line, character) { LiteralType = type; ExactType = exactType; CompiledValue = null; }
public ScriptVariable(int scope, ScriptVariableType varType, object val) { Scope = scope; VarType = varType; Value = val; }
public bool ConvertToVariable(ScriptVariableType varType, out object result) { unchecked { result = null; try { switch (varType) { case ScriptVariableType.Bool: if (LiteralType != ScriptLiteralTokenType.Boolean) { return(false); } result = Value.ToBoolean(false); break; case ScriptVariableType.Byte: if ((LiteralType & ScriptLiteralTokenType.Numeric) != ScriptLiteralTokenType.None) { result = Convert.ToByte(Value, NumberBase); } else if (LiteralType == ScriptLiteralTokenType.Char) { result = (byte)Value[0]; } else { return(false); } break; case ScriptVariableType.SByte: if ((LiteralType & ScriptLiteralTokenType.Numeric) != ScriptLiteralTokenType.None) { result = Convert.ToSByte(Value, NumberBase); } else if (LiteralType == ScriptLiteralTokenType.Char) { result = (sbyte)Value[0]; } else { return(false); } break; case ScriptVariableType.Short: if ((LiteralType & ScriptLiteralTokenType.Numeric) != ScriptLiteralTokenType.None) { result = Convert.ToInt16(Value, NumberBase); } else if (LiteralType == ScriptLiteralTokenType.Char) { result = (short)Value[0]; } else { return(false); } break; case ScriptVariableType.UShort: if ((LiteralType & ScriptLiteralTokenType.Numeric) != ScriptLiteralTokenType.None) { result = Convert.ToUInt16(Value, NumberBase); } else if (LiteralType == ScriptLiteralTokenType.Char) { result = (ushort)Value[0]; } else { return(false); } break; case ScriptVariableType.Int: if ((LiteralType & ScriptLiteralTokenType.Numeric) != ScriptLiteralTokenType.None) { result = Convert.ToInt32(Value, NumberBase); } else if (LiteralType == ScriptLiteralTokenType.Char) { result = (int)Value[0]; } else { return(false); } break; case ScriptVariableType.UInt: if ((LiteralType & ScriptLiteralTokenType.Numeric) != ScriptLiteralTokenType.None) { result = Convert.ToUInt32(Value, NumberBase); } else if (LiteralType == ScriptLiteralTokenType.Char) { result = (uint)Value[0]; } else { return(false); } break; case ScriptVariableType.Long: if ((LiteralType & ScriptLiteralTokenType.Numeric) != ScriptLiteralTokenType.None) { result = Convert.ToInt64(Value, NumberBase); } else if (LiteralType == ScriptLiteralTokenType.Char) { result = (long)Value[0]; } else { return(false); } break; case ScriptVariableType.ULong: if ((LiteralType & ScriptLiteralTokenType.Numeric) != ScriptLiteralTokenType.None) { result = Convert.ToUInt64(Value, NumberBase); } else if (LiteralType == ScriptLiteralTokenType.Char) { result = (ulong)Value[0]; } else { return(false); } break; case ScriptVariableType.Float: if ((LiteralType & ScriptLiteralTokenType.Numeric) != ScriptLiteralTokenType.None) { result = (NumberBase != 10) ? (float)Convert.ToInt32(Value, NumberBase) : Convert.ToSingle(Value); } else if (LiteralType == ScriptLiteralTokenType.Char) { result = (float)Value[0]; } else { return(false); } break; case ScriptVariableType.Double: if ((LiteralType & ScriptLiteralTokenType.Numeric) != ScriptLiteralTokenType.None) { result = (NumberBase != 10) ? (double)Convert.ToInt64(Value, NumberBase) : Convert.ToDouble(Value); } else if (LiteralType == ScriptLiteralTokenType.Char) { result = (double)Value[0]; } else { return(false); } break; case ScriptVariableType.Decimal: if ((LiteralType & ScriptLiteralTokenType.Numeric) != ScriptLiteralTokenType.None) { result = (NumberBase != 10) ? (decimal)Convert.ToInt64(Value, NumberBase) : Convert.ToDecimal(Value); } else if (LiteralType == ScriptLiteralTokenType.Char) { result = (decimal)Value[0]; } else { return(false); } break; case ScriptVariableType.Char: if ((LiteralType & ScriptLiteralTokenType.Numeric) != ScriptLiteralTokenType.None) { result = (char)Convert.ToUInt16(Value, NumberBase); } else if (LiteralType == ScriptLiteralTokenType.Char) { result = Value[0]; } else { return(false); } break; case ScriptVariableType.String: if (LiteralType == ScriptLiteralTokenType.Null) { result = (string)null; } else if (LiteralType == ScriptLiteralTokenType.String) { result = Value; } else { return(false); } break; default: return(false); } } catch { return(false); } } return(true); }