/* * The script to be converted * The script"s global scope * The scope under which we"re converting * * @throws ConversionException */ public TES5Target Convert(TES4Target target, TES5GlobalScope globalScope, TES5MultipleScriptsScope multipleScriptsScope) { TES4Script script = target.Script; TES5BlockList blockList = new TES5BlockList(); TES4BlockList parsedBlockList = script.BlockList; Dictionary <string, List <ITES5CodeBlock> > createdBlocks = new Dictionary <string, List <ITES5CodeBlock> >(); if (parsedBlockList != null) { foreach (TES4CodeBlock block in parsedBlockList.Blocks) { TES5BlockList newBlockList = this.blockFactory.CreateBlock(block, globalScope, multipleScriptsScope); foreach (ITES5CodeBlock newBlock in newBlockList.Blocks) { createdBlocks.AddNewListIfNotContainsKeyAndAddValueToList(newBlock.BlockName, newBlock); } } } //todo encapsulate it to a different class. bool isStandalone = target.OutputPath.Contains(Path.DirectorySeparatorChar + "Standalone" + Path.DirectorySeparatorChar); foreach (var createdBlock in createdBlocks) { var blockType = createdBlock.Key; var blocks = createdBlock.Value; if (blocks.Count > 1) { List <TES5FunctionCodeBlock> functions = new List <TES5FunctionCodeBlock>(); int i = 1; TES5ObjectCallArguments localScopeArguments = null; foreach (TES5CodeBlock block in blocks) { TES5FunctionScope newFunctionScope = new TES5FunctionScope(blockType + "_" + i.ToString()); foreach (var variable in block.FunctionScope.Variables.Values) { newFunctionScope.AddVariable(variable); } TES5FunctionCodeBlock function = new TES5FunctionCodeBlock(newFunctionScope, block.CodeScope, new TES5VoidType(), isStandalone); functions.Add(function); if (localScopeArguments == null) { localScopeArguments = new TES5ObjectCallArguments(); foreach (var variable in block.FunctionScope.Variables.Values) { localScopeArguments.Add(TES5ReferenceFactory.CreateReferenceToVariable(variable)); } } ++i; } //Create the proxy block. ITES5CodeBlock lastBlock = blocks.Last(); TES5EventCodeBlock proxyBlock = TES5BlockFactory.CreateEventCodeBlock(blockType, /* * //WTM: Change: block was used below, but block is out of scope. The PHP must have been using the last defined block from above. * //WTM: Change: PHP called "clone" below, but I'm not sure if this is necessary or would even operate the same in C#. */ lastBlock.FunctionScope); foreach (var function in functions) { blockList.Add(function); TES5ObjectCall functionCall = this.objectCallFactory.CreateObjectCall(TES5ReferenceFactory.CreateReferenceToSelf(globalScope), function.BlockName, multipleScriptsScope, localScopeArguments, false // hacky. ); proxyBlock.AddChunk(functionCall); } blockList.Add(proxyBlock); } else { ITES5CodeBlock block = blocks[0]; blockList.Add(block); } } TES5Target result = new TES5Target(new TES5Script(globalScope, blockList), target.OutputPath); return(result); }
public TES4OBScriptGrammar() : base(false) { __invoke("Script").Is("ScriptHeader", "Block+").Call((TES4ScriptHeader header, TES4BlockList blockList) => { return(new TES4Script(header, null, blockList)); }). Is("ScriptHeader", "VariableDeclaration+").Call((TES4ScriptHeader header, TES4VariableDeclarationList variableList) => { return(new TES4Script(header, variableList, null)); }). Is("ScriptHeader", "VariableDeclaration+", "Block+").Call((TES4ScriptHeader header, TES4VariableDeclarationList variableList, TES4BlockList blockList) => { return(new TES4Script(header, variableList, blockList)); }) #if NEWBT . Is("ScriptHeader").Call((TES4ScriptHeader header) =>//WTM: Change: I added this section to allow for a few more scripts to parsed (scripts that are just a header). { return(new TES4Script(header, null, null)); }) #endif ; __invoke("ScriptHeader").Is("ScriptHeaderToken", "ScriptName").Call((object headerToken, CommonToken scriptName) => { return(new TES4ScriptHeader(scriptName.Value)); }) ; __invoke("VariableDeclaration+").Is("VariableDeclaration+", "VariableDeclaration").Call((TES4VariableDeclarationList list, TES4VariableDeclaration variableDeclaration) => { list.Add(variableDeclaration); return(list); }). Is("VariableDeclaration").Call((TES4VariableDeclaration variableDeclaration) => { TES4VariableDeclarationList list = new TES4VariableDeclarationList(); list.Add(variableDeclaration); return(list); }) ; __invoke("VariableDeclaration").Is("VariableDeclarationType", "VariableName").Call((CommonToken variableDeclarationType, CommonToken variableName) => { return(new TES4VariableDeclaration(variableName.Value, TES4Type.GetFirst(variableDeclarationType.Value.ToLower()))); }) ; __invoke("Block+").Is("Block+", "Block").Call((TES4BlockList list, TES4CodeBlock blockDeclaration) => { list.Add(blockDeclaration); return(list); }). Is("Block").Call((TES4CodeBlock blockDeclaration) => { TES4BlockList list = new TES4BlockList(); list.Add(blockDeclaration); return(list); }) ; __invoke("Block").Is("BlockStart", "BlockType", "BlockParameter+", "Code+", "BlockEnd").Call((object blockStart, CommonToken blockType, TES4BlockParameterList blockParameters, TES4CodeChunks codeChunks, object blockEnd) => { return(new TES4CodeBlock(blockType.Value, blockParameters, codeChunks)); }). Is("BlockStart", "BlockType", "BlockParameter+", "BlockEnd").Call((object blockStart, CommonToken blockType, TES4BlockParameterList blockParameters, object blockEnd) => { return(new TES4CodeBlock(blockType.Value, blockParameters, null)); }) //rare empty block . Is("BlockStart", "BlockType", "Code+", "BlockEnd").Call((object blockStart, CommonToken blockType, TES4CodeChunks codeChunks, object blockEnd) => { return(new TES4CodeBlock(blockType.Value, null, codeChunks)); }). Is("BlockStart", "BlockType", "BlockEnd").Call((object blockStart, CommonToken blockType, object blockEnd) => { return(new TES4CodeBlock(blockType.Value, null, null)); }) ; //rare empty block __invoke("BlockParameter+").Is("BlockParameter+", "BlockParameter").Call((TES4BlockParameterList list, TES4BlockParameter blockParameter) => { list.Add(blockParameter); return(list); }). Is("BlockParameter").Call((TES4BlockParameter blockParameter) => { TES4BlockParameterList block = new TES4BlockParameterList(); block.Add(blockParameter); return(block); }) ; __invoke("BlockParameter").Is("BlockParameterToken").Call((CommonToken token) => { return(new TES4BlockParameter(token.Value)); }) ; this.CreateObscriptCodeParsingTree(); this.Start("Script"); }
public TES4OBScriptGrammar() : base(false) { __invoke("Script").Is("ScriptHeader", "Block+").Call((TES4ScriptHeader header, TES4BlockList blockList) => { return(new TES4Script(header, null, blockList)); }). Is("ScriptHeader", "VariableDeclaration+").Call((TES4ScriptHeader header, TES4VariableDeclarationList variableList) => { return(new TES4Script(header, variableList, null)); }). Is("ScriptHeader", "VariableDeclaration+", "Block+").Call((TES4ScriptHeader header, TES4VariableDeclarationList variableList, TES4BlockList blockList) => { return(new TES4Script(header, variableList, blockList)); }) ; __invoke("ScriptHeader").Is("ScriptHeaderToken", "ScriptName").Call((System.Func <object, CommonToken, TES4ScriptHeader>)((object headerToken, CommonToken scriptName) => { return(new TES4ScriptHeader((string)scriptName.Value)); })) ; __invoke("VariableDeclaration+").Is("VariableDeclaration+", "VariableDeclaration").Call((TES4VariableDeclarationList list, TES4VariableDeclaration variableDeclaration) => { list.add(variableDeclaration); return(list); }). Is("VariableDeclaration").Call((TES4VariableDeclaration variableDeclaration) => { TES4VariableDeclarationList list = new TES4VariableDeclarationList(); list.add(variableDeclaration); return(list); }) ; __invoke("VariableDeclaration").Is("VariableDeclarationType", "VariableName").Call((System.Func <CommonToken, CommonToken, TES4VariableDeclaration>)((CommonToken variableDeclarationType, CommonToken variableName) => { return(new TES4VariableDeclaration((string)variableName.Value, TES4Type.GetFirst((string)variableDeclarationType.Value.ToLower()))); })) ; __invoke("Block+").Is("Block+", "Block").Call((TES4BlockList list, TES4CodeBlock blockDeclaration) => { list.Add(blockDeclaration); return(list); }). Is("Block").Call((TES4CodeBlock blockDeclaration) => { TES4BlockList list = new TES4BlockList(); list.Add(blockDeclaration); return(list); }) ; __invoke("Block").Is("BlockStart", "BlockType", "BlockParameter+", "Code+", "BlockEnd").Call((System.Func <object, CommonToken, TES4BlockParameterList, TES4CodeChunks, object, TES4CodeBlock>)((object blockStart, CommonToken blockType, TES4BlockParameterList blockParameters, TES4CodeChunks codeChunks, object blockEnd) => { return(new TES4CodeBlock((string)blockType.Value, blockParameters, codeChunks)); })). Is("BlockStart", "BlockType", "BlockParameter+", "BlockEnd").Call((System.Func <object, CommonToken, TES4BlockParameterList, object, TES4CodeBlock>)((object blockStart, CommonToken blockType, TES4BlockParameterList blockParameters, object blockEnd) => { return(new TES4CodeBlock((string)blockType.Value, blockParameters, null)); })) //rare empty block . Is("BlockStart", "BlockType", "Code+", "BlockEnd").Call((System.Func <object, CommonToken, TES4CodeChunks, object, TES4CodeBlock>)((object blockStart, CommonToken blockType, TES4CodeChunks codeChunks, object blockEnd) => { return(new TES4CodeBlock((string)blockType.Value, null, codeChunks)); })). Is("BlockStart", "BlockType", "BlockEnd").Call((System.Func <object, CommonToken, object, TES4CodeBlock>)((object blockStart, CommonToken blockType, object blockEnd) => { return(new TES4CodeBlock((string)blockType.Value, null, null)); })) ; //rare empty block __invoke("BlockParameter+").Is("BlockParameter+", "BlockParameter").Call((TES4BlockParameterList list, TES4BlockParameter blockParameter) => { list.Add(blockParameter); return(list); }). Is("BlockParameter").Call((TES4BlockParameter blockParameter) => { TES4BlockParameterList block = new TES4BlockParameterList(); block.Add(blockParameter); return(block); }) ; __invoke("BlockParameter").Is("BlockParameterToken").Call((System.Func <CommonToken, TES4BlockParameter>)((CommonToken token) => { return(new TES4BlockParameter((string)token.Value)); })) ; this.createObscriptCodeParsingTree(); this.Start("Script"); }
public TES4Script(TES4ScriptHeader scriptHeader, TES4VariableDeclarationList declarationList = null, TES4BlockList blockList = null) { this.ScriptHeader = scriptHeader; this.VariableDeclarationList = declarationList; this.BlockList = blockList; }