public void GenericParse() { SemanticTypeActions<MockGenericTokenBase> actions = new SemanticTypeActions<MockGenericTokenBase>(grammar); actions.Initialize(); SemanticProcessor<MockGenericTokenBase> processor = new SemanticProcessor<MockGenericTokenBase>(new StringReader("-1+2+3*4-8"), actions); Assert.Equal(ParseMessage.Accept, processor.ParseAll()); Assert.IsAssignableFrom<MockGenericTokenBase>(processor.CurrentToken); }
static void Main(string[] args) { CompiledGrammar grammar = CompiledGrammar.Load(typeof(Token), "minim0.2.cgt"); SemanticTypeActions<Token> actions = new SemanticTypeActions<Token>(grammar); CodeGenerator.Init("test.exe"); TypeChecker.Init(); try { actions.Initialize(true); } catch (InvalidOperationException ex) { Console.Write(ex.Message); Console.ReadKey(true); return; } SemanticProcessor<Token> processor = new SemanticProcessor<Token>(new StreamReader(args[0]), actions); ParseMessage parseMessage = processor.ParseAll(); if (parseMessage == ParseMessage.Accept) { Console.WriteLine("Parsed successfully."); Program p = (Program)processor.CurrentToken; CodeGenerator.Complete(); } else { IToken token = processor.CurrentToken; Console.WriteLine("Error on line " + token.Position.Line + ".\n" + token.Position.ToString()); Console.WriteLine(string.Format("{0} {1}", "^".PadLeft((int)(token.Position.Index + 1)), parseMessage)); } }
public void ParseEmpty() { using (TestStringReader reader = new TestStringReader("")) { SemanticProcessor<TestToken> processor = new SemanticProcessor<TestToken>(reader, actions); Assert.Equal(ParseMessage.Accept, processor.ParseAll()); Assert.IsType<TestEmpty>(processor.CurrentToken); } }
public void RegisterSameMembraneType() { SemanticProcessor sp = new SemanticProcessor(); IMembrane membrane1 = sp.RegisterMembrane<TestMembrane>(); IMembrane membrane2 = sp.RegisterMembrane<TestMembrane>(); Assert.That(membrane1 == membrane2, "Expected the same membrane instance."); }
static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); sp = new SemanticProcessor(); sp.Processing += Processing; RouteHandlers routeHandlers = new RouteHandlers(sp); WebServer webServer = new WebServer(routeHandlers); webServer.Start("localhost", new int[] { 5001 }); form = new Form(); form.Location = new Point(100, 100); form.Size = new Size(500, 200); form.Text = "Stand Alone Runner"; tbLog = new TextBox(); tbLog.Multiline = true; tbLog.Dock = DockStyle.Fill; form.Controls.Add(tbLog); Application.Run(form); }
public object Exec(string input, ExecutionContext ctx) { var processor = new SemanticProcessor <GCToken>(new StringReader(input), actions); ParseMessage parseMessage = processor.ParseAll(); if (parseMessage == ParseMessage.Accept) { var statement = processor.CurrentToken as Expression; if (statement != null) { try { return(statement.GetValue(ctx)); } catch (FormulaSolverException ex) { return("??" + ex.Message); } } } else { IToken token = processor.CurrentToken; return(String.Format("??Syntax error at: {0} [{1}]", token.Position.Index, parseMessage)); } return(null); }
public object Exec(string input, ExecutionContext ctx) { var processor = new SemanticProcessor<GCToken>(new StringReader(input), actions); ParseMessage parseMessage = processor.ParseAll(); if (parseMessage == ParseMessage.Accept) { var statement = processor.CurrentToken as Expression; if (statement != null) { try { return statement.GetValue(ctx); } catch (FormulaSolverException ex) { return "??" + ex.Message; } } } else { IToken token = processor.CurrentToken; return String.Format("??Syntax error at: {0} [{1}]", token.Position.Index, parseMessage); } return null; }
public void RegisterMembraneType() { SemanticProcessor sp = new SemanticProcessor(); IMembrane membrane = sp.RegisterMembrane <TestMembrane>(); Assert.That(sp.Membranes.Contains(membrane), "Expected membrane instance."); }
private static void Main(string[] args) { Console.WriteLine("Language Scrappy Compiler 1.0"); if (args.Length == 0) { Console.WriteLine("usage: Scrappy <file.sp>"); return; } var grammar = CompiledGrammar.Load(typeof(Program), "Scrappy.egt"); var actions = new SemanticTypeActions <BaseToken>(grammar); try { actions.Initialize(true); } catch (InvalidOperationException ex) { Console.Write("Error: " + ex.Message); return; } try { var path = args[0]; var outputName = Path.GetFileNameWithoutExtension(path) + ".xml"; using (var reader = File.OpenText(path)) { var processor = new SemanticProcessor <BaseToken>(reader, actions); ParseMessage parseMessage = processor.ParseAll(); if (parseMessage == ParseMessage.Accept) { Console.WriteLine("Parsing done."); var compilationModel = new CompilationModel(File.ReadAllLines(path)); var start = (Start)processor.CurrentToken; start.Compile(compilationModel); // first classes, fields and methods needs to be compiled compilationModel.Compile(); // after that compile method body Console.WriteLine("Compiling done."); using (var outfile = new StreamWriter(outputName)) { outfile.Write(compilationModel.ToXml()); } // PrintAst(start); // only for debugging } else { IToken token = processor.CurrentToken; Console.WriteLine(token.Symbol); Console.WriteLine("Error: Line: {0} Column: {1} Error: {2}", token.Position.Line, token.Position.Column, parseMessage); } } } catch (Exception e) { Console.WriteLine("Error" + e.Message); } }
static void Main(string[] args) { ServiceManager sm = new ServiceManager(); SemanticProcessor semproc = new SemanticProcessor(); semproc.Initialize(sm); sm.RegisterSingleton <ISemanticProcessor>(semproc); var wsClient = new WebSocketClientService(); wsClient.Initialize(sm); sm.RegisterSingleton <IWebSocketClientService>(wsClient); semproc.Register <SocketMembrane, ServerSocketReceiver>(); // wsClient.Start("ws://127.0.0.1", 1000, "/wstest"); wsClient.Start("ws://24.105.201.179", 31415, "/wstest"); // wsClient.Start("ws://emsserver.club", 443, "/wstest"); // wsClient.Start("wss://24.105.201.179", 443, "/wstest"); // wsClient.Start("wss://emsserver.club", 443, "/wstest"); // Console.WriteLine("WS client connected to 127.0.0.1:1000"); wsClient.Send("Hello World!"); Console.ReadLine(); }
public void Skype_MessageStatus(IChatMessage message, TChatMessageStatus status) { Match output = Regex.Match(message.Body, @"^!(?:calc|eval) (.+)", RegexOptions.IgnoreCase | RegexOptions.Singleline); if (output.Success) { String exp = output.Groups[1].Value; SemanticProcessor <MathToken> processor = new SemanticProcessor <MathToken>(new StringReader(exp), actions); ParseMessage parseMessage = processor.ParseAll(); if (parseMessage == ParseMessage.Accept) { message.Chat.SendMessage( String.Format( "{0} = {1}", exp, ((Computable)processor.CurrentToken).GetValue())); } else { IToken token = processor.CurrentToken; message.Chat.SendMessage(string.Format("{0} ({1} on line {2}, column {3})", parseMessage, token.Symbol, token.Position.Line, token.Position.Column)); } } }
public Model(string someArgs) { gp = GraphematicProcessor.Instance; mp = MorphologyProcessor.Instance; sp = SyntaxProcessor.Instance; semP = new SemanticProcessor(); }
private static void Main(string[] args) { Console.WriteLine("Language Scrappy Compiler 1.0"); if (args.Length == 0) { Console.WriteLine("usage: Scrappy <file.sp>"); return; } var grammar = CompiledGrammar.Load(typeof(Program), "Scrappy.egt"); var actions = new SemanticTypeActions<BaseToken>(grammar); try { actions.Initialize(true); } catch (InvalidOperationException ex) { Console.Write("Error: " + ex.Message); return; } try { var path = args[0]; var outputName = Path.GetFileNameWithoutExtension(path) + ".xml"; using (var reader = File.OpenText(path)) { var processor = new SemanticProcessor<BaseToken>(reader, actions); ParseMessage parseMessage = processor.ParseAll(); if (parseMessage == ParseMessage.Accept) { Console.WriteLine("Parsing done."); var compilationModel = new CompilationModel(File.ReadAllLines(path)); var start = (Start)processor.CurrentToken; start.Compile(compilationModel); // first classes, fields and methods needs to be compiled compilationModel.Compile(); // after that compile method body Console.WriteLine("Compiling done."); using (var outfile = new StreamWriter(outputName)) { outfile.Write(compilationModel.ToXml()); } // PrintAst(start); // only for debugging } else { IToken token = processor.CurrentToken; Console.WriteLine(token.Symbol); Console.WriteLine("Error: Line: {0} Column: {1} Error: {2}", token.Position.Line, token.Position.Column, parseMessage); } } } catch (Exception e) { Console.WriteLine("Error" + e.Message); } }
public void ParseNull() { using (TestStringReader reader = new TestStringReader("NULL")) { SemanticProcessor<TestToken> processor = new SemanticProcessor<TestToken>(reader, actions); Assert.Equal(ParseMessage.Accept, processor.ParseAll()); Assert.IsType<TestEmpty>(processor.CurrentToken); Assert.False((processor.CurrentToken is TestSpecial) && ((TestSpecial)processor.CurrentToken).IsString); } }
public void RegisterSameMembraneType() { SemanticProcessor sp = new SemanticProcessor(); IMembrane membrane1 = sp.RegisterMembrane <TestMembrane>(); IMembrane membrane2 = sp.RegisterMembrane <TestMembrane>(); Assert.That(membrane1 == membrane2, "Expected the same membrane instance."); }
public void ParseSimpleExpression() { using (TestStringReader reader = new TestStringReader("100")) { SemanticProcessor<TestToken> processor = new SemanticProcessor<TestToken>(reader, actions); Assert.Equal(ParseMessage.Accept, processor.ParseAll()); Assert.IsAssignableFrom<TestValue>(processor.CurrentToken); TestValue value = (TestValue)processor.CurrentToken; Assert.Equal(100, value.Compute()); } }
public void ParseComplexExpression() { using (TestStringReader reader = new TestStringReader("((100+5.0)/\r\n(4.5+.5))-\r\n12345.4e+1")) { SemanticProcessor<TestToken> processor = new SemanticProcessor<TestToken>(reader, actions); Assert.Equal(ParseMessage.Accept, processor.ParseAll()); Assert.IsAssignableFrom<TestValue>(processor.CurrentToken); TestValue value = (TestValue)processor.CurrentToken; Assert.Equal(-123433.0, value.Compute()); } }
public void InitializerCalledForSemanticTypeConstruction() { bool initializerCalled = false; SemanticProcessor sp = new SemanticProcessor(); sp.Register <TestMembrane, TestReceptor>(); sp.ProcessInstance <TestMembrane, TestSemanticType>((t) => initializerCalled = true, true); Assert.That(initializerCalled, "Expected semantic type initializer to be called."); }
public void ReceptorDoesNotReceiveSemanticTypeOnAnotherMembrane() { callSuccess = false; SemanticProcessor sp = new SemanticProcessor(); sp.Register <TestMembrane>(new TestReceptor()); sp.ProcessInstance <TestMembrane2, TestSemanticType>(true); Assert.That(!callSuccess, "Expected TestReceptor.Process to NOT be called."); }
public void ReceptorOfInterfaceTypCalled() { callSuccess = false; SemanticProcessor sp = new SemanticProcessor(); sp.Register <TestMembrane>(new InterfaceTestReceptor()); sp.ProcessInstance <TestMembrane, InterfaceTestSemanticType>(true); Assert.That(callSuccess, "Expected TestReceptor.Process to be called."); }
public void BaseClassProcessCalled() { callSuccess = false; SemanticProcessor sp = new SemanticProcessor(); sp.Register <TestMembrane>(new DerivedTestReceptor()); sp.ProcessInstance <TestMembrane, TestSemanticType>(true); Assert.That(callSuccess, "Expected TestReceptor.Process to be called."); }
public void ReceptorReceivesSemanticTypeOnItsMembrane() { callSuccess = false; SemanticProcessor sp = new SemanticProcessor(); sp.Register <TestMembrane>(new TestReceptor()); sp.ProcessInstance <TestMembrane, TestSemanticType>(true); Assert.That(callSuccess, "Expected TestReceptor.Process to be called."); }
public void ProcessCallIsLogged() { stLogged = false; SemanticProcessor sp = new SemanticProcessor(); sp.Register<LoggerMembrane, LoggerReceptor>(); sp.Register<LoggerMembrane, ExceptionReceptor>(); sp.Register<TestMembrane, TestReceptor>(); sp.ProcessInstance<TestMembrane, TestSemanticType>(true); Assert.That(stLogged, "Expected Process call to be logged."); }
public void ExceptionIsLogged() { exLogged = false; SemanticProcessor sp = new SemanticProcessor(); sp.Register<LoggerMembrane, LoggerReceptor>(); sp.Register<LoggerMembrane, ExceptionReceptor>(); sp.Register<TestMembrane, TestReceptor>(); sp.ProcessInstance<TestMembrane, TypeThrowsException>(true); Assert.That(exLogged, "Expected Exception call to be logged."); }
public void RemoveType() { callSuccess = false; SemanticProcessor sp = new SemanticProcessor(); sp.Register <TestMembrane, TestReceptor>(); sp.RemoveTypeNotify <TestMembrane, TestReceptor, TestSemanticType>(); sp.ProcessInstance <TestMembrane, TestSemanticType>(true); Assert.That(!callSuccess, "Expected TestReceptor.Process to NOT be called."); }
public void DistributedComputation() { SemanticProcessor spOut = new SemanticProcessor(); SemanticProcessor spIn = new SemanticProcessor(); received = ""; OutboundDistributedComputingReceptor dcrOut = new OutboundDistributedComputingReceptor(4002); InboundDistributedComputingReceptor dcrIn = new InboundDistributedComputingReceptor(4002, spIn); // Create an "emitter" in which a semantic type emitted on the TestMembrane permeates // into the inner DistributedProcessMembrane for our test type. spOut.AddChild <TestMembrane, DistributedProcessMembrane>(); spOut.OutboundPermeableTo <TestMembrane, TestDistributedSemanticType>(); spOut.InboundPermeableTo <DistributedProcessMembrane, TestDistributedSemanticType>(); // The stateful DCR out lives in the distributed process membrane. spOut.Register <DistributedProcessMembrane>(dcrOut); // Create a "receiver" in which a semantic type is received on the inner DistributedProcessMembrane // and the test type permeates out to a "handler" receptor. spIn.AddChild <TestMembrane, DistributedProcessMembrane>(); spIn.OutboundPermeableTo <DistributedProcessMembrane, TestDistributedSemanticType>(); spIn.InboundPermeableTo <TestMembrane, TestDistributedSemanticType>(); // The stateful DCR in lives in the distributed process membrane. spIn.Register <DistributedProcessMembrane>(dcrIn); // The responding receptor lives in the TestMembrane spIn.Register <TestMembrane, TestReceptor>(); // Put a semantic type instance on the outbound side. spOut.ProcessInstance <TestMembrane, TestDistributedSemanticType>((t) => { t.Message = "Hello World"; }); // Wait a bit for threads to do their thing and Http posts to do their things. // System.Diagnostics.Debug.WriteLine("Waiting..."); // !*!*!*!* Sometimes this wait must be longer -- the unit test engine can really slow things down. // !*!*!*!* This is particularly true when running the test in the debugger! // !*!*!*!* If this delay isn't long enough for the server's message to be processed, you will get // !*!*!*!* errors related to accessing objects on an unloaded AppDomain. // !*!*!*!* In real life this woudn't happen -- this is an artifact of unit testing a complex // !*!*!*!* multi-threaded process. //Thread.Sleep(500); // Because we know it works, we could actually do this, which is particularly useful when we're // debugging and single stepping through code -- we do not want the test in this AppDomain // to exit prematurely! while (String.IsNullOrEmpty(received)) { Thread.Sleep(0); } Assert.That(received == "Hello World", "Expected to receive 'Hello World'"); }
public void DistributedComputation() { SemanticProcessor spOut = new SemanticProcessor(); SemanticProcessor spIn = new SemanticProcessor(); received = ""; OutboundDistributedComputingReceptor dcrOut = new OutboundDistributedComputingReceptor(4002); InboundDistributedComputingReceptor dcrIn = new InboundDistributedComputingReceptor(4002, spIn); // Create an "emitter" in which a semantic type emitted on the TestMembrane permeates // into the inner DistributedProcessMembrane for our test type. spOut.AddChild<TestMembrane, DistributedProcessMembrane>(); spOut.OutboundPermeableTo<TestMembrane, TestDistributedSemanticType>(); spOut.InboundPermeableTo<DistributedProcessMembrane, TestDistributedSemanticType>(); // The stateful DCR out lives in the distributed process membrane. spOut.Register<DistributedProcessMembrane>(dcrOut); // Create a "receiver" in which a semantic type is received on the inner DistributedProcessMembrane // and the test type permeates out to a "handler" receptor. spIn.AddChild<TestMembrane, DistributedProcessMembrane>(); spIn.OutboundPermeableTo<DistributedProcessMembrane, TestDistributedSemanticType>(); spIn.InboundPermeableTo<TestMembrane, TestDistributedSemanticType>(); // The stateful DCR in lives in the distributed process membrane. spIn.Register<DistributedProcessMembrane>(dcrIn); // The responding receptor lives in the TestMembrane spIn.Register<TestMembrane, TestReceptor>(); // Put a semantic type instance on the outbound side. spOut.ProcessInstance<TestMembrane, TestDistributedSemanticType>((t) => { t.Message = "Hello World"; }); // Wait a bit for threads to do their thing and Http posts to do their things. // System.Diagnostics.Debug.WriteLine("Waiting..."); // !*!*!*!* Sometimes this wait must be longer -- the unit test engine can really slow things down. // !*!*!*!* This is particularly true when running the test in the debugger! // !*!*!*!* If this delay isn't long enough for the server's message to be processed, you will get // !*!*!*!* errors related to accessing objects on an unloaded AppDomain. // !*!*!*!* In real life this woudn't happen -- this is an artifact of unit testing a complex // !*!*!*!* multi-threaded process. //Thread.Sleep(500); // Because we know it works, we could actually do this, which is particularly useful when we're // debugging and single stepping through code -- we do not want the test in this AppDomain // to exit prematurely! while (String.IsNullOrEmpty(received)) { Thread.Sleep(0); } Assert.That(received == "Hello World", "Expected to receive 'Hello World'"); }
public static bool Parse(Stream stream) { CompiledGrammar grammar = CompiledGrammar.Load(typeof(TestUnicode), "TestUnicode.egt"); // embedded resource SemanticTypeActions<TestToken> actions = new SemanticTypeActions<TestToken>(grammar); actions.Initialize(true); using (StreamReader reader = new StreamReader(stream)) // defaults to UTF-8 { SemanticProcessor<TestToken> processor = new SemanticProcessor<TestToken>(reader, actions); ParseMessage parseMessage = processor.ParseAll(); return (parseMessage == ParseMessage.Accept); } }
public void ProcessCallIsLogged() { stLogged = false; SemanticProcessor sp = new SemanticProcessor(); sp.Register <LoggerMembrane, LoggerReceptor>(); sp.Register <LoggerMembrane, ExceptionReceptor>(); sp.Register <TestMembrane, TestReceptor>(); sp.ProcessInstance <TestMembrane, TestSemanticType>(true); Assert.That(stLogged, "Expected Process call to be logged."); }
public void ReceptorTypeCreateDestroy() { constructorCalled = false; disposeCalled = false; SemanticProcessor sp = new SemanticProcessor(); sp.Register <TestMembrane, TestReceptor>(); sp.ProcessInstance <TestMembrane, TestSemanticType>(true); Assert.That(constructorCalled, "Expected constructor to be called."); Assert.That(disposeCalled, "Expected Dispose to be called."); }
public void ExceptionIsLogged() { exLogged = false; SemanticProcessor sp = new SemanticProcessor(); sp.Register <LoggerMembrane, LoggerReceptor>(); sp.Register <LoggerMembrane, ExceptionReceptor>(); sp.Register <TestMembrane, TestReceptor>(); sp.ProcessInstance <TestMembrane, TypeThrowsException>(true); Assert.That(exLogged, "Expected Exception call to be logged."); }
public void NotPermeableOut() { callSuccess = false; SemanticProcessor sp = new SemanticProcessor(); // sp.OutboundPermeableTo<InnerMembrane, TestSemanticType>(); sp.InboundPermeableTo<OuterMembrane, TestSemanticType>(); sp.AddChild<OuterMembrane, InnerMembrane>(); sp.Register<OuterMembrane, TestReceptor>(); sp.ProcessInstance<InnerMembrane, TestSemanticType>(true); Assert.That(!callSuccess, "Expected receptor in outer membrane to NOT receive the ST placed in the inner membrane."); }
public InAppRunner() { sp = new SemanticProcessor(); // membrane = new HopeMembrane(); // membrane = sp.RegisterMembrane<HopeMembrane>(); // membrane = new App.HopeMembrane(); membrane = sp.RegisterMembrane <App.HopeMembrane>(); sp.Processing += ProcessingSemanticType; }
public void ReceptorInstanceCreateDestroy() { SemanticProcessor sp = new SemanticProcessor(); sp.Register <TestMembrane>(new TestReceptor()); constructorCalled = false; disposeCalled = false; sp.ProcessInstance <TestMembrane, TestSemanticType>(true); Assert.That(!constructorCalled, "Expected constructor NOT to be called."); Assert.That(!disposeCalled, "Expected Dispose NOT to be called."); }
public void TypePermeatesIn() { callSuccess = false; SemanticProcessor sp = new SemanticProcessor(); sp.OutboundPermeableTo <OuterMembrane, TestSemanticType>(); sp.InboundPermeableTo <InnerMembrane, TestSemanticType>(); sp.AddChild <OuterMembrane, InnerMembrane>(); sp.Register <InnerMembrane, TestReceptor>(); sp.ProcessInstance <OuterMembrane, TestSemanticType>(true); Assert.That(callSuccess, "Expected receptor in inner membrane to process the ST placed in the outer membrane."); }
public void MultipleProcessCalls() { callSuccess = false; callSuccess2 = false; SemanticProcessor sp = new SemanticProcessor(); sp.Register <TestMembrane>(new TestReceptor()); sp.Register <TestMembrane>(new TestReceptor2()); sp.ProcessInstance <TestMembrane, TestSemanticType>(true); Assert.That(callSuccess, "Expected TestReceptor.Process to be called."); Assert.That(callSuccess2, "Expected TestReceptor2.Process to be called."); }
public void NotPermeableOut() { callSuccess = false; SemanticProcessor sp = new SemanticProcessor(); // sp.OutboundPermeableTo<InnerMembrane, TestSemanticType>(); sp.InboundPermeableTo <OuterMembrane, TestSemanticType>(); sp.AddChild <OuterMembrane, InnerMembrane>(); sp.Register <OuterMembrane, TestReceptor>(); sp.ProcessInstance <InnerMembrane, TestSemanticType>(true); Assert.That(!callSuccess, "Expected receptor in outer membrane to NOT receive the ST placed in the inner membrane."); }
public void ComplexTypePropertyProcessing() { simpleTypeProcessed = false; complexTypeProcessed = false; SemanticProcessor sp = new SemanticProcessor(); sp.Register <TestMembrane, ComplexReceptor>(); sp.Register <TestMembrane, SimpleReceptor>(); sp.ProcessInstance <TestMembrane, ComplexType>(true); Assert.That(complexTypeProcessed, "Expected ComplexReceptor.Process to be called."); Assert.That(simpleTypeProcessed, "Expected SimpleReceptor.Process to be called."); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Form1 form = new Form1(); SemProc = new SemanticProcessor(); SemProc.Register<LoggerMembrane, LoggingReceptor>(); // DistReceptor = new DistributedComputingReceptor(); // InitializeSemantics(form); Application.Run(form); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Form1 form = new Form1(); SemProc = new SemanticProcessor(); SemProc.Register <LoggerMembrane, LoggingReceptor>(); // DistReceptor = new DistributedComputingReceptor(); // InitializeSemantics(form); Application.Run(form); }
public static ParsedLine Parse(string line) { if (line == null) { throw new ArgumentNullException("line"); } using (StringReader reader = new StringReader(line)) { SemanticProcessor<CliToken> processor = new SemanticProcessor<CliToken>(reader, GetSemanticActions()); ParseMessage result = processor.ParseAll(); if (result != ParseMessage.Accept) { throw new FormatException(string.Format("The given string could not be parsed: {0} at position {1}", result, ((IToken)processor.CurrentToken).Position.Index+1)); } return (ParsedLine)processor.CurrentToken; } }
public void ReceptorInitialization() { receptorInitializerCalled = false; SemanticProcessor sp = new SemanticProcessor(); sp.Register <TestMembrane, TestReceptor>((ir) => { // Unfortunately, a cast is required, because ir is type declared as IReceptor // and I don't think it's possible to fix that because of the late callback. TestReceptor r = (TestReceptor)ir; r.AFlag = true; receptorInitializerCalled = true; }); sp.ProcessInstance <TestMembrane, TestSemanticType>(true); Assert.That(receptorInitializerCalled, "Expected TestReceptor initializer to be called to be called."); }
public static void Execute(string filePath) { if (grammarActions == null) { init(); } ExecutionContext.Reset(); //create default base context ExecutionContext.EnterLevel(); (ExecutionContext.Current.DefaultBroker as FileBlockBroker).SetLibFolder(DefaultBlocksPath); ScriptReader reader = new ScriptReader(filePath); SemanticProcessor<Token> processor = new SemanticProcessor<Token>(reader, grammarActions); ParseMessage parseMessage = processor.ParseAll(); reader.Close(); if (parseMessage != ParseMessage.Accept) { string newLine = "\n"; string errorMessage = parseMessage.ToString()+"."; string location = string.Format("Expecting one of following tokens at Line {0}, Column {1}:", processor.CurrentToken.Line, processor.CurrentToken.Column); string symbols = ""; foreach (Symbol s in processor.GetExpectedTokens()) { string txt = "\t'" + s.Name + "' of type " + s.Kind.ToString()+newLine; symbols += txt; } throw new Exception(errorMessage+newLine+location+newLine+symbols); } Optional<TokenList<CommandHandler>> optCommands = processor.CurrentToken as Optional<TokenList<CommandHandler>>; if (optCommands.HasValue) { TokenList<CommandHandler> commands = optCommands.Value; foreach (CommandHandler command in commands) { command.Execute(); } } }
public InboundDistributedComputingReceptor(int inboundPort, SemanticProcessor sp) { this.sp = sp; server = new Server(); server.OnRequest = (session, context) => { session.Authenticated = true; session.UpdateLastConnectionTime(); }; server.AddRoute(new Route() { Verb = Router.POST, Path = "/semanticType", Handler = new AnonymousRouteHandler(server, ProcessInboundSemanticType) }); server.Start("", inboundPort); }
//public static T Cast<T>(object o) //{ // return (T)o; //} static void Main(string[] args) { SemanticProcessor sp = new SemanticProcessor(); // AnotherType gets notified when instances of OneType are added to the pool. sp.Register <SurfaceMembrane, AReceptor>(); // auto register sp.Register <SurfaceMembrane, BReceptor>(); sp.Register <SurfaceMembrane, CReceptor>(); // Explicit register //sp.TypeNotify<AReceptor, OneType>(); //sp.TypeNotify<AReceptor, SecondType>(); OneType t1 = new OneType(); SecondType t2 = new SecondType(); SecondDerivedType t3 = new SecondDerivedType(); // object foo = Cast<SecondType>(t3); sp.ProcessInstance(sp.Surface, t1); sp.ProcessInstance(sp.Surface, t2); sp.ProcessInstance(sp.Surface, t3); Thread.Sleep(1000); // Wait for threaded processes to complete. Console.WriteLine("\r\nChained processing..."); sp.RemoveTypeNotify <SurfaceMembrane, AReceptor, IOneType>(); sp.RemoveTypeNotify <SurfaceMembrane, AReceptor, SecondType>(); sp.RemoveTypeNotify <SurfaceMembrane, BReceptor, OneType>(); sp.RemoveTypeNotify <SurfaceMembrane, CReceptor, SecondDerivedType>(); // Chaining... // auto register: sp.Register <SurfaceMembrane, Chain1>(); sp.Register <SurfaceMembrane, Chain2>(); // Explicit register: //sp.TypeNotify<Chain1, OneType>(); //sp.TypeNotify<Chain2, SecondType>(); sp.ProcessInstance(sp.Surface, new OneType()); Thread.Sleep(1000); // Wait for threaded processes to complete. }
public void Skype_MessageStatus(IChatMessage message, TChatMessageStatus status) { Match output = Regex.Match(message.Body, @"^!(?:calc|eval) (.+)", RegexOptions.IgnoreCase | RegexOptions.Singleline); if (output.Success) { String exp = output.Groups[1].Value; SemanticProcessor<MathToken> processor = new SemanticProcessor<MathToken>(new StringReader(exp), actions); ParseMessage parseMessage = processor.ParseAll(); if (parseMessage == ParseMessage.Accept) { message.Chat.SendMessage( String.Format( "{0} = {1}", exp, ((Computable)processor.CurrentToken).GetValue())); } else { IToken token = processor.CurrentToken; message.Chat.SendMessage(string.Format("{0} ({1} on line {2}, column {3})", parseMessage, token.Symbol, token.Position.Line, token.Position.Column)); } } }
static void Main(string[] args) { ServiceManager sm = new ServiceManager(); SemanticProcessor semproc = new SemanticProcessor(); semproc.Initialize(sm); sm.RegisterSingleton <ISemanticProcessor>(semproc); wsServer = new WebSocketServerService(); wsServer.Initialize(sm); sm.RegisterSingleton <IWebSocketServerService>(wsServer); semproc.Register <SocketMembrane, ServerSocketReceiver>(); wsServer.Start("127.0.0.1", 1000, "/wstest"); Console.WriteLine("WS listening on 127.0.0.1:1000"); Console.ReadLine(); wsServer.Stop(); }
private static void Main(string[] args) { Console.WriteLine("*** CALCULATOR SAMPLE *** (input formula, empty line terminates)"); CompiledGrammar grammar = CompiledGrammar.Load(typeof(CalculatorToken), "Calculator.cgt"); SemanticTypeActions<CalculatorToken> actions = new SemanticTypeActions<CalculatorToken>(grammar); try { actions.Initialize(true); } catch (InvalidOperationException ex) { Console.Write(ex.Message); Console.ReadKey(true); return; } for (string formula = Console.ReadLine(); !string.IsNullOrEmpty(formula); formula = Console.ReadLine()) { SemanticProcessor<CalculatorToken> processor = new SemanticProcessor<CalculatorToken>(new StringReader(formula), actions); ParseMessage parseMessage = processor.ParseAll(); if (parseMessage == ParseMessage.Accept) { Console.WriteLine(string.Format(NumberFormatInfo.InvariantInfo, "Result: {0}", ((Computable)processor.CurrentToken).GetValue())); } else { IToken token = processor.CurrentToken; Console.WriteLine(string.Format("{0} {1}", "^".PadLeft(token.Position.Column), parseMessage)); } } }
public void RegisterMembraneType() { SemanticProcessor sp = new SemanticProcessor(); IMembrane membrane = sp.RegisterMembrane<TestMembrane>(); Assert.That(sp.Membranes.Contains(membrane), "Expected membrane instance."); }
public void TypePermeatesAcross() { callSuccess = false; SemanticProcessor sp = new SemanticProcessor(); sp.OutboundPermeableTo<InnerMembrane, TestSemanticType>(); sp.InboundPermeableTo<OuterMembrane, TestSemanticType>(); sp.OutboundPermeableTo<OuterMembrane, TestSemanticType>(); sp.InboundPermeableTo<InnerMembrane2, TestSemanticType>(); sp.AddChild<OuterMembrane, InnerMembrane>(); sp.AddChild<OuterMembrane, InnerMembrane2>(); sp.Register<InnerMembrane2, TestReceptor>(); sp.ProcessInstance<InnerMembrane, TestSemanticType>(true); Assert.That(callSuccess, "Expected receptor in inner membrane to process the ST placed in the adjacent inner membrane."); }
public Runner() { sp = new SemanticProcessor(); membrane = new HopeMembrane(); sp.RegisterMembrane <HopeMembrane>(); }