Skip to content
This repository has been archived by the owner on Sep 5, 2020. It is now read-only.

Commit

Permalink
While statements work again. Break statements work now. You just can'…
Browse files Browse the repository at this point in the history
…t edit values that are declared outside the while loop so its useless at the moment.
  • Loading branch information
AzureKitsune committed Nov 26, 2011
1 parent 1f90c8a commit 702e852
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 38 deletions.
Binary file modified Mizu3.suo
Binary file not shown.
111 changes: 94 additions & 17 deletions Mizu3/DLR/DLRASTBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,40 @@ namespace Mizu3.DLR
using Microsoft.Scripting.Ast;
using AstUtils = Microsoft.Scripting.Ast.Utils;
using System.Reflection;
using System.IO;

/// <summary>
/// TODO: Update summary.
/// </summary>
public class DLRASTBuilder
{
public static System.Linq.Expressions.Expression[] Parse(string filename, ref LambdaBuilder main)
public static System.Linq.Expressions.Expression[] Parse(System.IO.FileInfo file, ref LambdaBuilder main)
{
var code = "";
var str = new StreamReader(file.OpenRead());
try
{
code = str.ReadToEnd();
}
catch (Exception)
{

}
finally
{
str.Close();
str.Dispose();
}
return Parse(code, ref main);
}
public static System.Linq.Expressions.Expression[] Parse(string source, ref LambdaBuilder main)
{
var statements = new List<Expression>();

{
var locals = new List<ParameterExpression>();

string src = System.IO.File.ReadAllText(filename);
string src = source;
var scanner = new Scanner();
var parser = new Parser(scanner);
var tree = parser.Parse(src);
Expand All @@ -46,15 +66,15 @@ public static System.Linq.Expressions.Expression[] Parse(string filename, ref La
return statements.ToArray();
}
}
private static Expression HandleStmt(ParseNode pn, ref LambdaBuilder func, ref List<ParameterExpression> locals, string src, LabelTarget loop = null)
private static Expression HandleStmt(ParseNode pn, ref LambdaBuilder func, ref List<ParameterExpression> locals, string src, LabelTarget label = null)
{
switch (pn.Token.Type)
{
case TokenType.LetStatement:
{
#region LET
var start = pn.GetLineAndCol(src);
var end = pn.GetLineAndColEnd(src);
/*var start = pn.GetLineAndCol(src);
var end = pn.GetLineAndColEnd(src); */

var nam = pn.Nodes[1].Token.Text;
Type ty = null;
Expand All @@ -78,7 +98,8 @@ private static Expression HandleStmt(ParseNode pn, ref LambdaBuilder func, ref L
case TokenType.FuncStatement:
{
//TODO: Implement this.

exp = HandleFunc(value, ref func);
ty = exp.Type;
break;
}
case TokenType.ArrayIndexExpr:
Expand Down Expand Up @@ -110,17 +131,19 @@ private static Expression HandleStmt(ParseNode pn, ref LambdaBuilder func, ref L
}
case TokenType.BreakStatement:
{
throw new NotImplementedException("Break Statements!");
if (loop == null)
//throw new NotImplementedException("Break Statements!");
if (label == null)
{
//TODO: Make this an error.
return null;
}
else
return Expression.Break(loop);
return Expression.Break(label);
}
case TokenType.WhileStatement:
{
#region While

Expression w = null;

var expr = pn.Nodes[2];
Expand All @@ -134,36 +157,46 @@ private static Expression HandleStmt(ParseNode pn, ref LambdaBuilder func, ref L
var loopfunc = AstUtils.Lambda(typeof(void), "Loop");
loopfunc.Locals.AddRange(locals);

var l = Expression.Label("LoopBreak");


//loopfunc.

var @l = Expression.Label("LoopBreak");

var bodyexp = new List<Expression>();

var bodylocs = new List<ParameterExpression>();
bodylocs.AddRange(locals);

if (body != null)
foreach (ParseNode p in body.Nodes)
bodyexp.Add(HandleStmt(p.Nodes[0], ref loopfunc, ref bodylocs, src,l));
bodyexp.Add(HandleStmt(p.Nodes[0], ref loopfunc, ref bodylocs, src, l));

loopfunc.Body = Expression.Block(bodylocs.ToArray(), bodyexp);

loopfunc.Body = Expression.Block(bodyexp);
w = AstUtils.While(op, loopfunc.MakeLambda(), Expression.Empty());


w = AstUtils.While(op, loopfunc.Body, Expression.Empty(), @l, null);


return w;
#endregion
}
case TokenType.VariableReassignmentStatement:
{
#region Variable Assignment
var vari = func.Locals.Find(it => it.Name == pn.Nodes[0].Token.Text);
if (vari == null)
throw new Exception(pn.Nodes[0].Token.Text + " doesn't exist! TODO: Implement error handling!");

var inner = pn.Nodes.Find(it => it.Token.Type == TokenType.ArrayIndexExpr);
if (inner == null)
{
//Normal variable assignment.
//Normal variable assignmenEt.

var right = pn.Nodes.Find(it => it.Token.Type == TokenType.Argument || it.Token.Type == TokenType.FuncStatement);

switch(right.Token.Type)

switch (right.Token.Type)
{
case TokenType.Argument:
return Expression.Assign(vari,
Expand All @@ -190,6 +223,7 @@ private static Expression HandleStmt(ParseNode pn, ref LambdaBuilder func, ref L
rexp,
typeof(object)));
}
#endregion
}
case TokenType.OutStatement:
{
Expand All @@ -210,10 +244,49 @@ private static Expression HandleStmt(ParseNode pn, ref LambdaBuilder func, ref L
return Expression.Call(writeLine);
}
}
case TokenType.RetStatement:
{
break;
}

}
return null;
}
private static Expression HandleFunc(ParseNode value, ref LambdaBuilder func)
{
var inner = value.Nodes[0];
var name = value.Parent.Nodes[1];
var param = value.Nodes.FindAll(it => it.Token.Type == TokenType.Parameter);
var stmts = value.Nodes.Find(it => it.Token.Type == TokenType.Statement || it.Token.Type == TokenType.Statements);

var meth = AstUtils.Lambda(typeof(object), name.Token.Text);

foreach (ParseNode par in param)
HandleParameter(par, ref meth);

var locs = new List<ParameterExpression>();

if (stmts.Token.Type == TokenType.Statement)
meth.Body = HandleStmt(stmts.Nodes[0], ref meth, ref locs, "", null);
else
{
var lab = Expression.Label("Return");
var st = new List<Expression>();
foreach (var s in stmts.Nodes)
st.Add(HandleStmt(s.Nodes[0], ref meth, ref locs, "", lab));


st.Add(Expression.Return(lab));
meth.Body = Expression.Block(st);
}


return meth.MakeLambda();
}
private static ParameterExpression HandleParameter(ParseNode value, ref LambdaBuilder func)
{
return func.Parameter(typeof(object), value.Nodes[0].Token.Text);
}
private static Expression HandleNonMathExpr(ParseNode value, Expression left, Expression right)
{
switch (value.Token.Type)
Expand Down Expand Up @@ -335,6 +408,8 @@ private static Expression HandleMathExpr(ParseNode pn, ref LambdaBuilder func)
break;
case TokenType.IDENTIFIER:
lexp = func.Locals.Find(it => it.Name == left.Token.Text);
if (lexp == null)
lexp = func.Parameters.Find(it => it.Name == left.Token.Text);
break;
}

Expand All @@ -353,6 +428,8 @@ private static Expression HandleMathExpr(ParseNode pn, ref LambdaBuilder func)
break;
case TokenType.IDENTIFIER:
rexp = func.Locals.Find(it => it.Name == right.Token.Text);
if (rexp == null)
rexp = func.Parameters.Find(it => it.Name == right.Token.Text);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion Mizu3/DLR/DLRCompiler/DLRCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void Compile(Compiler.CompilerParameters info)



var exps = Mizu3.DLR.DLRASTBuilder.Parse(file, ref main);
var exps = Mizu3.DLR.DLRASTBuilder.Parse(new FileInfo(file), ref main);
main.Body = Expression.Block(typeof(void),exps);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
// </copyright>
// -----------------------------------------------------------------------

namespace Mizu3.DLRCompiler
namespace Mizu3.DLR
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Scripting.Runtime;
using System.Linq.Expressions;
using AstUtils = Microsoft.Scripting.Ast.Utils;

/// <summary>
/// TODO: Update summary.
Expand All @@ -20,7 +22,17 @@ public class MizuLanguageContext : LanguageContext
public MizuLanguageContext(ScriptDomainManager sc): base(sc){}
public override Microsoft.Scripting.ScriptCode CompileSourceCode(Microsoft.Scripting.SourceUnit sourceUnit, Microsoft.Scripting.CompilerOptions options, Microsoft.Scripting.ErrorSink errorSink)
{
throw new NotImplementedException();
var lamb = AstUtils.Lambda(typeof(void), "exec");

lamb.Body = Expression.Block(Expression.Block(DLRASTBuilder.Parse(sourceUnit.GetCode(), ref lamb)));

return new LegacyScriptCode(
lamb.MakeLambda(), sourceUnit);
}

public override int ExecuteProgram(Microsoft.Scripting.SourceUnit program)
{
return base.ExecuteProgram(program);
}
}
}
32 changes: 21 additions & 11 deletions Mizu3/Example.miz
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let x = 5;
/*let x = 5;
let y = 6;
let z = - + x y 2;
out z;
Expand All @@ -15,15 +15,25 @@ out myArray[0];
out myArray[1];
out myArray[2];
out myArray[3];
out myArray[- 5 1];
out myArray[- 5 1]; */

/*let fib = fun(n) -> {
out "Test from function!";
};*/
let fib = fun(n) -> {
let x = [+ n 1];
x[0] = 0;
x[1] = 1;

let i = 2;
while(i != n) {
x[i] = x[- i 1] + x[- i 2];
i++;
}
return x;
};

let this = true;
while(this != false) {
out "LOL";
let w = 0;
this = true;
}
/*let this = "string";
while(this != "pie") {
out "Test";
this = "pie";
break;
}
out this; */
8 changes: 5 additions & 3 deletions Mizu3/Mizu3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<Compile Include="Compiler\TypeResolver.cs" />
<Compile Include="DLR\DLRASTBuilder.cs" />
<Compile Include="DLR\DLRCompiler\DLRCompiler.cs" />
<Compile Include="DLR\DLRCompiler\MizuLanguageContext.cs" />
<Compile Include="DLR\MizuLanguageContext.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down Expand Up @@ -113,11 +113,13 @@
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="DLR\DLRInterpreter\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
<UserProperties BuildVersion_BuildVersioningStyle="None.None.Increment.None" BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" />
<UserProperties BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" BuildVersion_BuildVersioningStyle="None.None.Increment.None" />
</VisualStudio>
</ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
21 changes: 19 additions & 2 deletions Mizu3/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,44 @@
using System.Linq;
using System.Text;
using Mizu3.Compiler;
using Microsoft.Scripting.Hosting;

namespace Mizu3
{
class Program
{
static void Main(string[] args)
{

CompilerParameters info = new CompilerParameters();
info.AssemblyName = "Example";
info.AssemblyName = "Example";

info.MainClass = "Default"; /* If not found,
* the compiler will search for a suitable method in the assembly
* and use the first one it finds.
*/
info.SourceCodeFiles = new string[]{args[0]};
info.SourceCodeFiles = new string[]{args[0]};
info.OutputFilename = new System.IO.FileInfo(args[0]).DirectoryName + "/Example.exe";
info.IsDebugMode = true;


new DLR.DLRCompiler.DLRCompiler().Compile(info);
object br = null;


/*ScriptRuntimeSetup info = new ScriptRuntimeSetup();
// Create runtime
ScriptRuntime runtime = new ScriptRuntime(info);
// Load Engine
ScriptEngine engine = runtime.GetEngine("miz");
// Execute command
ScriptSource src = engine.CreateScriptSourceFromString("out \"Hello World\";");
src.Execute(); */

// Shutdown engine
}
}
}
4 changes: 2 additions & 2 deletions Mizu3/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.180.0")]
[assembly: AssemblyFileVersion("1.0.180.0")]
[assembly: AssemblyVersion("1.0.212.0")]
[assembly: AssemblyFileVersion("1.0.212.0")]
Binary file modified Mizu3/bin/Debug/Example.pdb
Binary file not shown.

0 comments on commit 702e852

Please sign in to comment.