Negative Vorzeichen bei VOR

This commit is contained in:
2025-10-12 22:41:13 +02:00
parent 9864c1965f
commit ec4d7dc26e
45 changed files with 76 additions and 13 deletions

View File

@ -11,5 +11,12 @@ public enum VarType {
// Unsigned integers (alternative names) // Unsigned integers (alternative names)
USINT = 10, UINT = 11, UDINT = 12, ULINT = 13, USINT = 10, UINT = 11, UDINT = 12, ULINT = 13,
// Floating point // Floating point
REAL = 14, LREAL = 15 REAL = 14, LREAL = 15,
// Date/time types
TIME = 16, // Zeitdauer -> TimeSpan
DATE = 17, // Datum -> DateTime
TIME_OF_DAY = 18, // Tageszeit (TOD) -> TimeSpan / DateTime
TOD = TIME_OF_DAY,
DATE_AND_TIME = 19,// Datum + Uhrzeit (DT) -> DateTime
DT = DATE_AND_TIME
} }

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("STCompiler.Common")] [assembly: System.Reflection.AssemblyCompanyAttribute("STCompiler.Common")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d6c0c1ee3d20a8c9c034ddc1705666c59490293b")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9864c1965fa3f1dc2950b7afd816891e75f6857d")]
[assembly: System.Reflection.AssemblyProductAttribute("STCompiler.Common")] [assembly: System.Reflection.AssemblyProductAttribute("STCompiler.Common")]
[assembly: System.Reflection.AssemblyTitleAttribute("STCompiler.Common")] [assembly: System.Reflection.AssemblyTitleAttribute("STCompiler.Common")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
14757fc67d41cb1b12bbed6ec99ff57fbdd6b5433a8962bdc97d3b144274ced1 3ee5798b43eb2bdc5cb3333bf67081acc7fb5bfbfd1c25a96138387cd1048786

View File

@ -167,6 +167,7 @@ public class StLexer {
public List<CompileError> Errors = new(); public List<CompileError> Errors = new();
public StLexer(string s){src=s;} public StLexer(string s){src=s;}
char Peek()=> i<src.Length?src[i]:'\0'; char Peek()=> i<src.Length?src[i]:'\0';
char Peek2()=> i+1<src.Length?src[i+1]:'\0';
char Next(){ char Next(){
if (i >= src.Length) return '\0'; if (i >= src.Length) return '\0';
char c = src[i++]; char c = src[i++];
@ -180,6 +181,18 @@ public class StLexer {
while (char.IsWhiteSpace(Peek())) Next(); while (char.IsWhiteSpace(Peek())) Next();
if (Peek()=='\0') return new Token(TokType.EOF,"", currentLine); if (Peek()=='\0') return new Token(TokType.EOF,"", currentLine);
// Skip line comments starting with '//'
if (Peek() == '/' && Peek2() == '/') {
// consume '//'
Next(); Next();
// skip until end of line or EOF
while (Peek() != '\0' && Peek() != '\n') Next();
// consume newline if present
if (Peek() == '\n') Next();
// restart tokenization after the comment
return NextToken();
}
if (char.IsLetter(Peek())||Peek()=='_'){ if (char.IsLetter(Peek())||Peek()=='_'){
var sb=new StringBuilder(); var sb=new StringBuilder();
int startLine = currentLine; int startLine = currentLine;
@ -558,6 +571,22 @@ public class StParser {
Expr? ParsePrimary(){ Expr? ParsePrimary(){
int startLine = cur.Line; int startLine = cur.Line;
// Support unary + and -
if (cur.Type == TokType.PLUS || cur.Type == TokType.MINUS) {
var sign = cur.Type;
Next();
var p = ParsePrimary();
if (p == null) return null;
Expr zero;
if (p.Type == VarType.REAL || p.Type == VarType.LREAL) {
zero = new RealExpr(0.0, p.Type);
} else {
zero = new IntExpr(0, p.Type);
}
var op = sign == TokType.PLUS ? TokType.PLUS : TokType.MINUS;
return new BinaryExpr(zero, op, p);
}
switch(cur.Type) { switch(cur.Type) {
case TokType.INT: case TokType.INT:
if (!long.TryParse(cur.Text, out var v)) { if (!long.TryParse(cur.Text, out var v)) {

View File

@ -3,14 +3,41 @@ VAR
a UINT := 0; a UINT := 0;
b UINT := 0; b UINT := 0;
i UINT := 0; i UINT := 0;
sum DINT := 0;
flag BOOL := 0; // FALSE -> 0
x REAL := 1.5;
y LREAL := 2.5;
cnt INT := 0;
END_VAR END_VAR
WHILE a < 5 DO // Einfacher If-Test
a := a + 1; IF a = 0 THEN
a := 1;
END_IF;
// While-Schleife mit arithmetischen Ausdrücken
WHILE cnt < 3 DO
cnt := cnt + 1;
b := b + cnt * 2;
END_WHILE; END_WHILE;
FOR i := 1 TO 10 DO // For-Schleife mit BY und verschachteltem IF
b := b + i; FOR i := 1 TO 10 BY 2 DO
sum := sum + i;
IF i <> 5 THEN
flag := 1; // TRUE -> 1
ELSE
flag := 0; // FALSE -> 0
END_IF;
END_FOR; END_FOR;
// Abwärts-Schleife (negativer BY-Wert)
FOR i := 5 TO 1 BY -1 DO
sum := sum + i;
b := b + (i * (cnt + 1));
END_FOR;
// Rechnen mit Gleitkommazahlen
x := x + y / 2.0;
END_PROGRAM END_PROGRAM

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("STCompiler.Compiler")] [assembly: System.Reflection.AssemblyCompanyAttribute("STCompiler.Compiler")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d6c0c1ee3d20a8c9c034ddc1705666c59490293b")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9864c1965fa3f1dc2950b7afd816891e75f6857d")]
[assembly: System.Reflection.AssemblyProductAttribute("STCompiler.Compiler")] [assembly: System.Reflection.AssemblyProductAttribute("STCompiler.Compiler")]
[assembly: System.Reflection.AssemblyTitleAttribute("STCompiler.Compiler")] [assembly: System.Reflection.AssemblyTitleAttribute("STCompiler.Compiler")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
a424a1c3ee1d7810475125cddba739ca260f9cec29125bc53f7c5b943f8f805a f6dd1650f122ef522a0e29b4e2791479bb04684921f3ea39b7363b57cd76e656

Binary file not shown.

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("STCompiler.Disassembler")] [assembly: System.Reflection.AssemblyCompanyAttribute("STCompiler.Disassembler")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d6c0c1ee3d20a8c9c034ddc1705666c59490293b")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9864c1965fa3f1dc2950b7afd816891e75f6857d")]
[assembly: System.Reflection.AssemblyProductAttribute("STCompiler.Disassembler")] [assembly: System.Reflection.AssemblyProductAttribute("STCompiler.Disassembler")]
[assembly: System.Reflection.AssemblyTitleAttribute("STCompiler.Disassembler")] [assembly: System.Reflection.AssemblyTitleAttribute("STCompiler.Disassembler")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
464c162666b4bc2ea38f251b8581cce943c86b051338cfd40945bd712973ca04 1d7c20312d485399a6349304512afd19a40ff18597fb7dff3871190dd81cd69c

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("STCompiler.Simulator")] [assembly: System.Reflection.AssemblyCompanyAttribute("STCompiler.Simulator")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+d6c0c1ee3d20a8c9c034ddc1705666c59490293b")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9864c1965fa3f1dc2950b7afd816891e75f6857d")]
[assembly: System.Reflection.AssemblyProductAttribute("STCompiler.Simulator")] [assembly: System.Reflection.AssemblyProductAttribute("STCompiler.Simulator")]
[assembly: System.Reflection.AssemblyTitleAttribute("STCompiler.Simulator")] [assembly: System.Reflection.AssemblyTitleAttribute("STCompiler.Simulator")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
1f912a4d9abdccc3c72995cc47c6abca27b37eb53a3bef8d54e15f74186522d1 7a1166fec5b2204048691a4c5ed6c88cb952b3b293ec9ef79ad85cd86bbe7e09