Skip to content

Commit

Permalink
Merge pull request #2 from sergey-raevskiy/csharp
Browse files Browse the repository at this point in the history
C# версия компилятора и ВМ
  • Loading branch information
sayon authored Nov 21, 2016
2 parents 79e2e1b + a8241b0 commit 007759e
Show file tree
Hide file tree
Showing 15 changed files with 1,275 additions and 0 deletions.
26 changes: 26 additions & 0 deletions dwarf-cs/dwarf-cs.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dwarf.core", "dwarf.core\dwarf.core.csproj", "{FD95899A-79B4-4424-A531-92F0935AC39C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tests", "tests\tests.csproj", "{E48C4A1A-A404-466F-9F66-5D9A9C80896B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FD95899A-79B4-4424-A531-92F0935AC39C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FD95899A-79B4-4424-A531-92F0935AC39C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD95899A-79B4-4424-A531-92F0935AC39C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD95899A-79B4-4424-A531-92F0935AC39C}.Release|Any CPU.Build.0 = Release|Any CPU
{E48C4A1A-A404-466F-9F66-5D9A9C80896B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E48C4A1A-A404-466F-9F66-5D9A9C80896B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E48C4A1A-A404-466F-9F66-5D9A9C80896B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E48C4A1A-A404-466F-9F66-5D9A9C80896B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
153 changes: 153 additions & 0 deletions dwarf-cs/dwarf.core/BytecodeBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using System;
using System.Collections.Generic;

namespace dwarf.core
{
public class BytecodeBuilder
{
private List<byte> bytecode = new List<byte>();

private Dictionary<Label, int> labels = new Dictionary<Label, int>();
private Dictionary<Label, List<int>> fixups = new Dictionary<Label, List<int>>();

private int LabelOffset(Label label)
{
if (labels.ContainsKey(label))
return labels[label] - (bytecode.Count + 4);

if (!fixups.ContainsKey(label))
fixups.Add(label, new List<int>());

fixups[label].Add(bytecode.Count);
return bytecode.Count + 4;
}

private void DoFixups(List<int> offsets, int address)
{
foreach (var offset in offsets)
{
var pc = BitConverter.ToInt32(bytecode.GetRange(offset, 4).ToArray(), 0);
var b = BitConverter.GetBytes(address - pc);

for (int i = 0; i < 4; i++)
{
bytecode[offset + i] = b[i];
}
}
}

public List<byte> Bytecode
{
get { return bytecode; }
}

public BytecodeBuilder halt()
{
bytecode.Add(0x00);
return this;
}

public BytecodeBuilder iadd()
{
bytecode.Add(0x01);
return this;
}

public BytecodeBuilder isub()
{
bytecode.Add(0x02);
return this;
}

public BytecodeBuilder imul()
{
bytecode.Add(0x03);
return this;
}

public BytecodeBuilder idiv()
{
bytecode.Add(0x04);
return this;
}

public BytecodeBuilder dup()
{
bytecode.Add(0x05);
return this;
}

public BytecodeBuilder ipushc(long c)
{
bytecode.Add(0x06);
bytecode.AddRange(BitConverter.GetBytes(c));
return this;
}

public BytecodeBuilder ipushreg(short reg)
{
bytecode.Add(0x07);
bytecode.AddRange(BitConverter.GetBytes(reg));
return this;
}

public BytecodeBuilder ipopreg(short reg)
{
bytecode.Add(0x08);
bytecode.AddRange(BitConverter.GetBytes(reg));
return this;
}

public BytecodeBuilder jmp(Label label)
{
bytecode.Add(0x09);
bytecode.AddRange(BitConverter.GetBytes(LabelOffset(label)));
return this;
}

public BytecodeBuilder jz(Label label)
{
bytecode.Add(0x0A);
bytecode.AddRange(BitConverter.GetBytes(LabelOffset(label)));
return this;
}

public BytecodeBuilder jnz(Label label)
{
bytecode.Add(0x0B);
bytecode.AddRange(BitConverter.GetBytes(LabelOffset(label)));
return this;
}

public BytecodeBuilder icmp()
{
bytecode.Add(0x0C);
return this;
}

public BytecodeBuilder eq()
{
bytecode.Add(0x0D);
return this;
}

public BytecodeBuilder print()
{
bytecode.Add(0x0E);
return this;
}

public BytecodeBuilder label(Label label)
{
if (fixups.ContainsKey(label))
{
DoFixups(fixups[label], bytecode.Count);
fixups.Remove(label);
}

labels.Add(label, bytecode.Count);

return this;
}
}
}
6 changes: 6 additions & 0 deletions dwarf-cs/dwarf.core/Label.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace dwarf.core
{
public class Label
{
}
}
36 changes: 36 additions & 0 deletions dwarf-cs/dwarf.core/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("dwarf.core")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("dwarf.core")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("71dc51e8-79d2-4c7f-8de8-c2ff5c0fb6fd")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// 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.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
110 changes: 110 additions & 0 deletions dwarf-cs/dwarf.core/Vm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace dwarf.core
{
public class Vm
{
private byte[] bytecode;
private readonly StringWriter printer;

private Stack<long> stack = new Stack<long>();
private bool halted;
private int pc;

public Vm(IEnumerable<byte> bytecode, StringWriter printer)
{
this.bytecode = bytecode.ToArray();
this.printer = printer;
}

public void Run()
{
pc = 0;
halted = false;

while (!halted)
{
var instruction = bytecode[pc++];

switch (instruction)
{
case 0x00:
halted = true;
break;

case 0x01:
stack.Push(stack.Pop() + stack.Pop());
break;

case 0x02:
{
var b = stack.Pop();
var a = stack.Pop();

stack.Push(a - b);
break;
}

case 0x03:
stack.Push(stack.Pop() * stack.Pop());
break;

case 0x05:
stack.Push(stack.Peek());
break;

case 0x06:
stack.Push(BitConverter.ToInt64(bytecode, pc));
pc += sizeof(long);
break;

case 0x09:
{
var off = BitConverter.ToInt32(bytecode, pc);
pc += sizeof (int);

pc = pc + off;
break;
}

case 0x0A:
{
var off = BitConverter.ToInt32(bytecode, pc);
pc += sizeof (int);

if (stack.Pop() == 0)
pc = pc + off;
break;
}

case 0x0B:
{
var off = BitConverter.ToInt32(bytecode, pc);
pc += sizeof(int);

if (stack.Pop() != 0)
pc = pc + off;
break;
}

case 0x0D:
{
var a = stack.Pop();
var b = stack.Pop();

stack.Push(a == b ? 1 : 0);

break;
}

case 0x0E:
printer.WriteLine(stack.Pop());
break;
}
}
}
}
}
58 changes: 58 additions & 0 deletions dwarf-cs/dwarf.core/dwarf.core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{FD95899A-79B4-4424-A531-92F0935AC39C}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>dwarf.core</RootNamespace>
<AssemblyName>dwarf.core</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BytecodeBuilder.cs" />
<Compile Include="Label.cs" />
<Compile Include="lang\Lexer.cs" />
<Compile Include="lang\Tokens.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Vm.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Loading

0 comments on commit 007759e

Please sign in to comment.