This commit is contained in:
2025-10-11 23:42:21 +02:00
commit 0e549b6e0a
32 changed files with 584 additions and 0 deletions

25
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,25 @@
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net8.0/Compiler.dll",
"args": [
"input.st",
"out.bin"
],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}

43
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,43 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Compiler.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/Compiler.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/Compiler.csproj",
"input.st",
"out.bin"
],
"problemMatcher": "$msCompile"
}
]
}

10
Compiler.csproj Normal file
View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

24
Compiler.sln Normal file
View File

@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Compiler", "Compiler.csproj", "{CBEA9558-E4AD-DA70-5319-E69E934D0501}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CBEA9558-E4AD-DA70-5319-E69E934D0501}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CBEA9558-E4AD-DA70-5319-E69E934D0501}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBEA9558-E4AD-DA70-5319-E69E934D0501}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CBEA9558-E4AD-DA70-5319-E69E934D0501}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {27C531AE-201E-4E9C-9ACC-BDCA8ADD46DC}
EndGlobalSection
EndGlobal

201
Program.cs Normal file
View File

@ -0,0 +1,201 @@
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
class Program {
static int Main(string[] args) {
if (args.Length < 2) {
Console.WriteLine("Usage: StEmitter <input.st> <output.bin>");
return 1;
}
var input = File.ReadAllText(args[0]);
var parser = new StParser(input);
var prog = parser.ParseProgram();
var emitter = new BytecodeEmitter();
emitter.Compile(prog);
File.WriteAllBytes(args[1], emitter.BuildBinary());
Console.WriteLine($"Wrote {args[1]}: consts={emitter.ConstantsCount}, vars={emitter.VarCount}, code={emitter.CodeLength}");
return 0;
}
}
// === AST & TYPES ===
public enum VarType { Int8=1, Int16=2, Int32=3, Byte=4, Bool=5 }
public abstract class StNode{}
public class ProgramNode:StNode{ public List<VarDecl> Vars=new(); public List<Stmt> Stmts=new(); }
public class VarDecl:StNode{ public string Name; public VarType Type; public Expr? Init; }
public abstract class Stmt:StNode{}
public class AssignStmt:Stmt{ public string Target; public Expr Expr; }
public abstract class Expr:StNode{}
public class IntExpr:Expr{ public int Value; public IntExpr(int v){Value=v;} }
public class VarExpr:Expr{ public string Name; public VarExpr(string n){Name=n;} }
public class BinaryExpr:Expr{ public Expr L; public Expr R; public TokType Op; public BinaryExpr(Expr l,TokType op,Expr r){L=l;Op=op;R=r;} }
// === LEXER ===
public enum TokType { IDENT, INT, ASSIGN, SEMI, LPAREN, RPAREN, PLUS, MINUS, MUL, DIV, PROGRAM, VAR, END_VAR, END_PROGRAM, EOF }
public class Token { public TokType Type; public string Text; public Token(TokType t,string s){Type=t;Text=s;} }
public class StLexer {
private readonly string src; private int i;
public StLexer(string s){src=s;}
char Peek()=> i<src.Length?src[i]:'\0';
char Next()=> i<src.Length?src[i++]:'\0';
public Token NextToken(){
while(char.IsWhiteSpace(Peek())) Next();
if (Peek()=='\0') return new Token(TokType.EOF,"");
if (char.IsLetter(Peek())||Peek()=='_'){
var sb=new StringBuilder();
while(char.IsLetterOrDigit(Peek())||Peek()=='_') sb.Append(Next());
var s=sb.ToString();
switch(s.ToUpperInvariant()){
case "PROGRAM":return new Token(TokType.PROGRAM,s);
case "VAR":return new Token(TokType.VAR,s);
case "END_VAR":return new Token(TokType.END_VAR,s);
case "END_PROGRAM":return new Token(TokType.END_PROGRAM,s);
default:return new Token(TokType.IDENT,s);
}
}
if (char.IsDigit(Peek())){
var sb=new StringBuilder();
while(char.IsDigit(Peek())) sb.Append(Next());
return new Token(TokType.INT,sb.ToString());
}
if (Peek()==':'){ Next(); if(Peek()=='='){Next(); return new Token(TokType.ASSIGN,":=");} }
char c=Next();
return c switch {
';'=>new Token(TokType.SEMI,";"),
'('=>new Token(TokType.LPAREN,"("),
')'=>new Token(TokType.RPAREN,")"),
'+'=>new Token(TokType.PLUS,"+"),
'-'=>new Token(TokType.MINUS,"-"),
'*'=>new Token(TokType.MUL,"*"),
'/'=>new Token(TokType.DIV,"/"),
_=>throw new Exception($"Unexpected char '{c}'")
};
}
}
// === PARSER ===
public class StParser {
StLexer lex; Token cur;
public StParser(string s){lex=new StLexer(s);cur=lex.NextToken();}
void Next()=>cur=lex.NextToken();
void Expect(TokType t){if(cur.Type!=t)throw new Exception($"Expected {t}, got {cur.Type}");Next();}
public ProgramNode ParseProgram(){
var p=new ProgramNode();
Expect(TokType.PROGRAM);
if(cur.Type==TokType.IDENT) Next();
if(cur.Type==TokType.VAR){
Next();
while(cur.Type==TokType.IDENT) p.Vars.Add(ParseVarDecl());
Expect(TokType.END_VAR);
}
while(cur.Type!=TokType.END_PROGRAM&&cur.Type!=TokType.EOF)
p.Stmts.Add(ParseStmt());
Expect(TokType.END_PROGRAM);
return p;
}
VarDecl ParseVarDecl(){
string name=cur.Text; Expect(TokType.IDENT);
string tname=cur.Text.ToLowerInvariant(); Expect(TokType.IDENT);
VarType vt=tname switch {
"int8"=>VarType.Int8,"int16"=>VarType.Int16,"int32"=>VarType.Int32,
"byte"=>VarType.Byte,"bool"=>VarType.Bool,_=>throw new Exception($"Unknown type {tname}")
};
Expr? init=null;
if(cur.Type==TokType.ASSIGN){Next();init=ParseExpr();}
Expect(TokType.SEMI);
return new VarDecl{Name=name,Type=vt,Init=init};
}
Stmt ParseStmt(){
if(cur.Type==TokType.IDENT){
var n=cur.Text;Next();Expect(TokType.ASSIGN);
var e=ParseExpr();Expect(TokType.SEMI);
return new AssignStmt{Target=n,Expr=e};
}
throw new Exception($"Invalid stmt start {cur.Type}");
}
Expr ParseExpr(){return ParseAddSub();}
Expr ParseAddSub(){var l=ParseMulDiv();while(cur.Type==TokType.PLUS||cur.Type==TokType.MINUS){var op=cur.Type;Next();var r=ParseMulDiv();l=new BinaryExpr(l,op,r);}return l;}
Expr ParseMulDiv(){var l=ParsePrimary();while(cur.Type==TokType.MUL||cur.Type==TokType.DIV){var op=cur.Type;Next();var r=ParsePrimary();l=new BinaryExpr(l,op,r);}return l;}
Expr ParsePrimary(){
if(cur.Type==TokType.INT){int v=int.Parse(cur.Text);Next();return new IntExpr(v);}
if(cur.Type==TokType.IDENT){string n=cur.Text;Next();return new VarExpr(n);}
if(cur.Type==TokType.LPAREN){Next();var e=ParseExpr();Expect(TokType.RPAREN);return e;}
throw new Exception($"Unexpected token {cur.Type}");
}
}
// === BYTECODE EMITTER ===
public class BytecodeEmitter {
List<int> consts=new(); Dictionary<string,Symbol> syms=new(); List<byte> code=new();
class Symbol{public string Name;public VarType Type;public int Index;}
public int ConstantsCount=>consts.Count; public int VarCount=>syms.Count; public int CodeLength=>code.Count;
public void Compile(ProgramNode p){
int idx=0;
// Pass 1: Definiere Variablen (ohne Init)
foreach(var v in p.Vars)
syms[v.Name]=new Symbol{Name=v.Name,Type=v.Type,Index=idx++};
// Pass 2: Generiere Initialisierungscode
foreach(var v in p.Vars){
if(v.Init!=null){
EmitExpr(v.Init);
EmitByte(0x03); EmitU16((ushort)syms[v.Name].Index);
}
}
// Main-Code
foreach(var s in p.Stmts)
EmitStmt(s);
EmitByte(0xF0);
}
void EmitStmt(Stmt s){
if(s is AssignStmt a){
EmitExpr(a.Expr);
EmitByte(0x03); EmitU16((ushort)syms[a.Target].Index);
}
}
void EmitExpr(Expr e){
switch(e){
case IntExpr ie:
int ci=AddConst(ie.Value); EmitByte(0x01); EmitU16((ushort)ci); break;
case VarExpr ve:
if(!syms.ContainsKey(ve.Name))
throw new Exception($"Unknown variable {ve.Name}");
int vi=syms[ve.Name].Index; EmitByte(0x02); EmitU16((ushort)vi); break;
case BinaryExpr be:
EmitExpr(be.L); EmitExpr(be.R);
EmitByte(be.Op switch {
TokType.PLUS=>0x10, TokType.MINUS=>0x11, TokType.MUL=>0x12, TokType.DIV=>0x13,
_=>throw new Exception("bad op")
});
break;
default: throw new Exception("bad expr");
}
}
int AddConst(int v){int i=consts.IndexOf(v);if(i>=0)return i;consts.Add(v);return consts.Count-1;}
void EmitByte(byte b)=>code.Add(b);
void EmitU16(ushort v){code.Add((byte)(v&0xFF));code.Add((byte)(v>>8));}
public byte[] BuildBinary(){
using var ms=new MemoryStream();
var w=new BinaryWriter(ms);
w.Write(Encoding.ASCII.GetBytes("STBC"));
w.Write((ushort)1);
w.Write((ushort)consts.Count);
foreach(var c in consts) w.Write(c);
w.Write((ushort)syms.Count);
var types=new byte[syms.Count];
foreach(var kv in syms) types[kv.Value.Index]=(byte)kv.Value.Type;
foreach(var b in types) w.Write(b);
w.Write((ushort)code.Count); w.Write(code.ToArray());
return ms.ToArray();
}
}

BIN
bin/Debug/net8.0/Compiler Executable file

Binary file not shown.

View File

@ -0,0 +1,23 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v8.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v8.0": {
"Compiler/1.0.0": {
"runtime": {
"Compiler.dll": {}
}
}
}
},
"libraries": {
"Compiler/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,12 @@
{
"runtimeOptions": {
"tfm": "net8.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "8.0.0"
},
"configProperties": {
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}

View File

@ -0,0 +1,7 @@
PROGRAM MyProg
VAR
a;
x;
END_VAR
x := a + 5;
END_PROGRAM

9
input.st Normal file
View File

@ -0,0 +1,9 @@
PROGRAM Demo
VAR
a int16 := 5;
b byte := a + 3;
c int32 := b * 2;
END_VAR
a := a + c;
END_PROGRAM

View File

@ -0,0 +1,67 @@
{
"format": 1,
"restore": {
"/home/martin/Projekte/c#/Compiler/Compiler/Compiler.csproj": {}
},
"projects": {
"/home/martin/Projekte/c#/Compiler/Compiler/Compiler.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/home/martin/Projekte/c#/Compiler/Compiler/Compiler.csproj",
"projectName": "Compiler",
"projectPath": "/home/martin/Projekte/c#/Compiler/Compiler/Compiler.csproj",
"packagesPath": "/home/martin/.nuget/packages/",
"outputPath": "/home/martin/Projekte/c#/Compiler/Compiler/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/home/martin/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net8.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"downloadDependencies": [
{
"name": "Microsoft.AspNetCore.App.Ref",
"version": "[8.0.20, 8.0.20]"
}
],
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.120/PortableRuntimeIdentifierGraph.json"
}
}
}
}
}

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/home/martin/.nuget/packages/</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/home/martin/.nuget/packages/</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.8.1</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="/home/martin/.nuget/packages/" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />

View File

@ -0,0 +1,4 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v8.0", FrameworkDisplayName = ".NET 8.0")]

View File

@ -0,0 +1,22 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Compiler")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("Compiler")]
[assembly: System.Reflection.AssemblyTitleAttribute("Compiler")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class.

View File

@ -0,0 +1 @@
595ce70373ddd00c09969aa302b9639c0cb84849045f0786c137462a6cbd7825

View File

@ -0,0 +1,13 @@
is_global = true
build_property.TargetFramework = net8.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.InvariantGlobalization =
build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = Compiler
build_property.ProjectDir = /home/martin/Projekte/c#/Compiler/Compiler/
build_property.EnableComHosting =
build_property.EnableGeneratedComInterfaceComImportInterop =

View File

@ -0,0 +1,8 @@
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;

Binary file not shown.

View File

@ -0,0 +1 @@
e6875acff382b4d0405949e656b89286b801b5ee697f860245d5bfc0093ae0a1

View File

@ -0,0 +1,14 @@
/home/martin/Projekte/c#/Compiler/Compiler/bin/Debug/net8.0/Compiler
/home/martin/Projekte/c#/Compiler/Compiler/bin/Debug/net8.0/Compiler.deps.json
/home/martin/Projekte/c#/Compiler/Compiler/bin/Debug/net8.0/Compiler.runtimeconfig.json
/home/martin/Projekte/c#/Compiler/Compiler/bin/Debug/net8.0/Compiler.dll
/home/martin/Projekte/c#/Compiler/Compiler/bin/Debug/net8.0/Compiler.pdb
/home/martin/Projekte/c#/Compiler/Compiler/obj/Debug/net8.0/Compiler.GeneratedMSBuildEditorConfig.editorconfig
/home/martin/Projekte/c#/Compiler/Compiler/obj/Debug/net8.0/Compiler.AssemblyInfoInputs.cache
/home/martin/Projekte/c#/Compiler/Compiler/obj/Debug/net8.0/Compiler.AssemblyInfo.cs
/home/martin/Projekte/c#/Compiler/Compiler/obj/Debug/net8.0/Compiler.csproj.CoreCompileInputs.cache
/home/martin/Projekte/c#/Compiler/Compiler/obj/Debug/net8.0/Compiler.dll
/home/martin/Projekte/c#/Compiler/Compiler/obj/Debug/net8.0/refint/Compiler.dll
/home/martin/Projekte/c#/Compiler/Compiler/obj/Debug/net8.0/Compiler.pdb
/home/martin/Projekte/c#/Compiler/Compiler/obj/Debug/net8.0/Compiler.genruntimeconfig.cache
/home/martin/Projekte/c#/Compiler/Compiler/obj/Debug/net8.0/ref/Compiler.dll

Binary file not shown.

View File

@ -0,0 +1 @@
6b5d8c2d7157f68f44a5343b0bc0f4d9b4bb672dc0df047e587bb062cfab53c4

Binary file not shown.

BIN
obj/Debug/net8.0/apphost Executable file

Binary file not shown.

Binary file not shown.

Binary file not shown.

72
obj/project.assets.json Normal file
View File

@ -0,0 +1,72 @@
{
"version": 3,
"targets": {
"net8.0": {}
},
"libraries": {},
"projectFileDependencyGroups": {
"net8.0": []
},
"packageFolders": {
"/home/martin/.nuget/packages/": {}
},
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/home/martin/Projekte/c#/Compiler/Compiler/Compiler.csproj",
"projectName": "Compiler",
"projectPath": "/home/martin/Projekte/c#/Compiler/Compiler/Compiler.csproj",
"packagesPath": "/home/martin/.nuget/packages/",
"outputPath": "/home/martin/Projekte/c#/Compiler/Compiler/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/home/martin/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net8.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"downloadDependencies": [
{
"name": "Microsoft.AspNetCore.App.Ref",
"version": "[8.0.20, 8.0.20]"
}
],
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/8.0.120/PortableRuntimeIdentifierGraph.json"
}
}
}
}

10
obj/project.nuget.cache Normal file
View File

@ -0,0 +1,10 @@
{
"version": 2,
"dgSpecHash": "KIcVXcbXzuWQqdDXRD6T4jld1MppHDYJAAqZWyhE3TCjsZCWQHOYop3xnJIkOLAx4BWQhE7dCbfzWKvC3EIHVA==",
"success": true,
"projectFilePath": "/home/martin/Projekte/c#/Compiler/Compiler/Compiler.csproj",
"expectedPackageFiles": [
"/home/martin/.nuget/packages/microsoft.aspnetcore.app.ref/8.0.20/microsoft.aspnetcore.app.ref.8.0.20.nupkg.sha512"
],
"logs": []
}

BIN
out.bin Normal file

Binary file not shown.