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

@ -167,6 +167,7 @@ public class StLexer {
public List<CompileError> Errors = new();
public StLexer(string s){src=s;}
char Peek()=> i<src.Length?src[i]:'\0';
char Peek2()=> i+1<src.Length?src[i+1]:'\0';
char Next(){
if (i >= src.Length) return '\0';
char c = src[i++];
@ -180,6 +181,18 @@ public class StLexer {
while (char.IsWhiteSpace(Peek())) Next();
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()=='_'){
var sb=new StringBuilder();
int startLine = currentLine;
@ -558,6 +571,22 @@ public class StParser {
Expr? ParsePrimary(){
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) {
case TokType.INT:
if (!long.TryParse(cur.Text, out var v)) {

View File

@ -3,14 +3,41 @@ VAR
a UINT := 0;
b 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
WHILE a < 5 DO
a := a + 1;
// Einfacher If-Test
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;
FOR i := 1 TO 10 DO
b := b + i;
// For-Schleife mit BY und verschachteltem IF
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;
// 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

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("STCompiler.Compiler")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[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.AssemblyTitleAttribute("STCompiler.Compiler")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
a424a1c3ee1d7810475125cddba739ca260f9cec29125bc53f7c5b943f8f805a
f6dd1650f122ef522a0e29b4e2791479bb04684921f3ea39b7363b57cd76e656

Binary file not shown.