public void Bolha() { #region script var script = @"função ordenar(v) variável i, j, t, tam tam = v.tamanho para t = 1 até tam para i = 1 até tam j = i-1 se v[j] > v[i] então aux = v[i] v[i] = v[j] v[j] = aux fim fim fim retorne v fim"; #endregion var engine = new Motor(); engine.Compilar(script); var r = (object[])engine.Executar("ordenar", new object[] { new object[] { 4, 6, 2, 9, 5 } }); Assert.AreEqual(2, Convert.ToInt32(r[0])); }
private static string GetAsm(string script) { var engine = new Motor(); engine.Compilar(script); return engine.RuntimeContext.Runnable.InstrucStream.ToString(); }
public void Recursion_Call() { var engine = new Motor(); engine.Load(new Bytecode(GetLines())); Assert.AreEqual(9, engine.Executar("pow", new object[] {3, 2} )); Assert.AreEqual(27, engine.Executar("pow", new object[] { 3, 3 } )); Assert.AreEqual(16, engine.Executar("pow", new object[] { 2, 4 } )); Assert.AreEqual(125, engine.Executar("pow", new object[] { 5, 3 } )); }
private static void Execute(string script) { var engine = new Motor(); engine.Instalar(new WebModulo()); engine.Instalar(new HtmlModulo()); var modules = RetrieveModules(); foreach (var module in modules) engine.Instalar(module); engine.Compilar(script); engine.Executar("page", new Infra(HttpContext.Current)); }
public void Should_Bind_CSharp_Object_Property2() { var script = @" função email(usuario) variável m m = usuario.Email fim "; var engine = new Motor(new Npc()); engine.Compilar(script); engine.Executar("email", new Usuario {Email = "ricardoborgesAtGmailDotCom"}); Assert.AreEqual("ricardoborgesAtGmailDotCom", engine.RuntimeContext.GetSymbolValue("email_m_0")); }
public void Should_Bind_CSharp_Object_Property() { var script = @" função calcular(venda) variável total total = venda.valor + 10 fim "; var engine = new Motor(new Npc()); engine.Compilar(script); engine.Executar("calcular", new Venda {valor = 20}); Assert.AreEqual(30, engine.RuntimeContext.GetSymbolValue("calcular_total_0")); }
protected Motor CreateEngine(string script, SymbolTable model) { var engine = new Motor(); engine.Instalar(new WebModulo()); engine.Instalar(new HtmlModulo()); var modules = RetrieveModules(); foreach (var module in modules) engine.Instalar(module); engine.Compilar(script); if (model != null) engine.RuntimeContext.Runnable.ScriptSymbolTable = model; return engine; }
public void CPF() { #region script var script = @"função valida(cpf) variável vet vet = vetorizar(cpf) se vet.tamanho < 11 retorne falso fim se validaDigito(vet, vet[9], 9) == falso retorne falso fim se validaDigito(vet, vet[10], 10) == falso retorne falso fim retorne verdadeiro fim função validaDigito(vet, dig, tam) variável soma, seq, digcalc, i, num, r soma = 0 seq = tam + 1 para i = 0 até tam num = vet[i] soma = soma + num * seq seq = seq - 1 fim r = soma % 11 se r < 2 digcalc = 0 senão digcalc = 11 - r fim se digcalc == dig retorne verdadeiro fim retorne falso fim"; #endregion var engine = new Motor(); engine.Compilar(script); var r = engine.Executar("valida", "11111111111"); Assert.AreEqual(r, true); }
public void Propriedade() { #region script var script = @"função nome(lista, i) variável p p = lista[i] retorne p.Nome fim"; #endregion var engine = new Motor(); engine.Compilar(script); var lista = new object[] { new Pessoa { Nome = "Teste1" }, new Pessoa { Nome = "Teste2" }, new Pessoa { Nome = "Teste3" } }; var nome = engine.Executar("nome", new object[] {lista, 1}); Assert.AreEqual("Teste2", nome); }
public void Preco() { Thread.CurrentThread.CurrentCulture = new CultureInfo("pt-BR"); var script = @"função melhor(a, g) se a < g * 0.7 retorne ""Álcool"" senão retorne ""Gasolina"" fim fim"; var engine = new Motor(new Npc()); engine.Compilar(script); var r = engine.Executar("melhor", new object[]{3, 3}); Assert.AreEqual("Gasolina", r); }
public void Fatorial() { #region script var script = @" função fatorial(x) se x == 0 retorne 1 fim retorne fatorial(x - 1) * x fim"; #endregion var engine = new Motor(); engine.Compilar(script); var r = engine.Executar("fatorial", 3); Assert.AreEqual(r, 6); }
private static void Run(string filename, string function, string parameters) { if (!FileExists(filename)) return; var engine = new Motor(); engine.Load(new NPortugol.Npc().CompilarArquivo(filename)); engine.Instalar(new ConsoleModulo()); if (string.IsNullOrEmpty(function)) engine.Executar(); else { var list = new List<object>(); foreach (var parameter in parameters.Split(',')) { list.Add(parameter.Trim()); } engine.Executar(function, list.ToArray()); } Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("----------------------------------------"); Console.WriteLine(string.Format("{0} executado com sucesso. <ENTER>", filename)); }