/** * * @author seagle * @date 2024-7-22 * @Description 块语句处理 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MECF.Framework.UI.Core.DxfScript { public class BlockSentence : Sentence { private List _sentences = new List(); public int SentencesCount { get { return _sentences.Count; } } //解析多条语句 public override void Create(StringReader reader) { KeywordType kw; while ((kw = reader.Next()) != KeywordType.KW_EOF) { if (kw == KeywordType.KW_IF) { //if语句 reader.rollback(kw); IfSentence ifSentence = new IfSentence(); ifSentence.Create(reader); _sentences.Add(ifSentence); } else if (kw == KeywordType.KW_VARIABLE) { //赋值语句 reader.rollback(kw); SetSentence setSentence = new SetSentence(); setSentence.Create(reader); _sentences.Add(setSentence); } else if (kw == KeywordType.KW_FUNCTION) { //函数语句 reader.rollback(kw); FunSentence setSentence = new FunSentence(); setSentence.Create(reader); _sentences.Add(setSentence); } else if (kw == KeywordType.KW_RETURN) { //return语句 reader.rollback(kw); ReturnSentence returnSentence = new ReturnSentence(); returnSentence.Create(reader); _sentences.Add(returnSentence); } else if (kw == KeywordType.KW_BRACE_RIGHT) { break; } else if (kw == KeywordType.KW_SEP) { continue; } else { throw new Exception("Unexpected keyword:" + kw); } } } public override void Execute() { foreach (Sentence sentence in _sentences) { if (sentence is IfSentence) { IfSentence ifSentence = sentence as IfSentence; ifSentence.Execute(); if (ifSentence.ShouldReturn) { ShouldReturn = true; ReturnVar = ifSentence.ReturnVar; return; } } else if (sentence is SetSentence) { SetSentence setSentence = sentence as SetSentence; setSentence.Execute(); //ShouldReturn = true; } else if (sentence is FunSentence) { FunSentence funSentence = sentence as FunSentence; funSentence.Execute(); //ShouldReturn = true; } else if (sentence is BlockSentence) { BlockSentence blockSentence = sentence as BlockSentence; blockSentence.Execute(); if (blockSentence.ShouldReturn) { ShouldReturn = true; ReturnVar = blockSentence.ReturnVar; return; } } else if (sentence is ReturnSentence) { ReturnSentence returnSentence = sentence as ReturnSentence; returnSentence.Execute(); ShouldReturn = true; ReturnVar = returnSentence.ReturnVar; return; } } } } }