binary expressions
This commit is contained in:
@@ -1,22 +1,7 @@
|
||||
import "c";
|
||||
|
||||
struct Human {
|
||||
age: int64 = 0;
|
||||
name: string;
|
||||
}
|
||||
|
||||
global func main() {
|
||||
while true {
|
||||
let dad = new Human
|
||||
{
|
||||
name = "John";
|
||||
};
|
||||
|
||||
printName(dad);
|
||||
if 3 / 3 == 1 {
|
||||
puts("uwu");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func printName(human: Human) {
|
||||
puts(human.name);
|
||||
}
|
||||
@@ -313,7 +313,218 @@ public class Generator
|
||||
|
||||
private string GenerateBinaryExpression(BinaryExpressionNode binaryExpression)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var left = GenerateExpression(binaryExpression.Left);
|
||||
var right = GenerateExpression(binaryExpression.Right);
|
||||
var outputLabel = GenName();
|
||||
|
||||
switch (binaryExpression.Operator)
|
||||
{
|
||||
case BinaryExpressionOperator.Equal:
|
||||
{
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int64) && binaryExpression.Right.Type.Equals(NubType.Int64))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w ceql {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int32) && binaryExpression.Right.Type.Equals(NubType.Int32))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w ceqw {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.String) && binaryExpression.Right.Type.Equals(NubType.String))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w call $nub_strcmp(l {left}, l {right})");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Bool) && binaryExpression.Right.Type.Equals(NubType.Bool))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w ceqw {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BinaryExpressionOperator.NotEqual:
|
||||
{
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int64) && binaryExpression.Right.Type.Equals(NubType.Int64))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w cnel {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int32) && binaryExpression.Right.Type.Equals(NubType.Int32))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w cnew {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.String) && binaryExpression.Right.Type.Equals(NubType.String))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w call $nub_strcmp(l {left}, l {right})");
|
||||
_builder.AppendLine($" %{outputLabel} =w xor %{outputLabel}, 1");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Bool) && binaryExpression.Right.Type.Equals(NubType.Bool))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w cnew {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BinaryExpressionOperator.GreaterThan:
|
||||
{
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int64) && binaryExpression.Right.Type.Equals(NubType.Int64))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w csgtl {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int32) && binaryExpression.Right.Type.Equals(NubType.Int32))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w csgtw {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Bool) && binaryExpression.Right.Type.Equals(NubType.Bool))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w csgtw {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BinaryExpressionOperator.GreaterThanOrEqual:
|
||||
{
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int64) && binaryExpression.Right.Type.Equals(NubType.Int64))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w csgel {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int32) && binaryExpression.Right.Type.Equals(NubType.Int32))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w csgew {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Bool) && binaryExpression.Right.Type.Equals(NubType.Bool))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w csgew {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BinaryExpressionOperator.LessThan:
|
||||
{
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int64) && binaryExpression.Right.Type.Equals(NubType.Int64))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w csltl {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int32) && binaryExpression.Right.Type.Equals(NubType.Int32))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w csltw {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Bool) && binaryExpression.Right.Type.Equals(NubType.Bool))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w csltw {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BinaryExpressionOperator.LessThanOrEqual:
|
||||
{
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int64) && binaryExpression.Right.Type.Equals(NubType.Int64))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w cslel {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int32) && binaryExpression.Right.Type.Equals(NubType.Int32))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w cslew {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Bool) && binaryExpression.Right.Type.Equals(NubType.Bool))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w cslew {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BinaryExpressionOperator.Plus:
|
||||
{
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int64) && binaryExpression.Right.Type.Equals(NubType.Int64))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =l add {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int32) && binaryExpression.Right.Type.Equals(NubType.Int32))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w add {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BinaryExpressionOperator.Minus:
|
||||
{
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int64) && binaryExpression.Right.Type.Equals(NubType.Int64))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =l sub {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int32) && binaryExpression.Right.Type.Equals(NubType.Int32))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w sub {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BinaryExpressionOperator.Multiply:
|
||||
{
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int64) && binaryExpression.Right.Type.Equals(NubType.Int64))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =l mul {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int32) && binaryExpression.Right.Type.Equals(NubType.Int32))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w mul {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BinaryExpressionOperator.Divide:
|
||||
{
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int64) && binaryExpression.Right.Type.Equals(NubType.Int64))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =l div {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
|
||||
if (binaryExpression.Left.Type.Equals(NubType.Int32) && binaryExpression.Right.Type.Equals(NubType.Int32))
|
||||
{
|
||||
_builder.AppendLine($" %{outputLabel} =w div {left}, {right}");
|
||||
return $"%{outputLabel}";
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
throw new NotSupportedException($"Binary operator {binaryExpression.Operator} for types left: {binaryExpression.Left.Type}, right: {binaryExpression.Right.Type} not supported");
|
||||
}
|
||||
|
||||
private string GenerateIdentifier(IdentifierNode identifier)
|
||||
@@ -396,13 +607,13 @@ public class Generator
|
||||
|
||||
var parameters = results.Select(p => $"{QbeTypeName(p.Item2)} {p.Item1}");
|
||||
|
||||
var output = GenName("var");
|
||||
var output = GenName();
|
||||
_builder.AppendLine($" %{output} ={QbeTypeName(funcCall.Type)} call ${funcCall.FuncCall.Name}({string.Join(", ", parameters)})");
|
||||
|
||||
return $"%{output}";
|
||||
}
|
||||
|
||||
private string GenName(string prefix)
|
||||
private string GenName(string prefix = "var")
|
||||
{
|
||||
var index = _prefixIndexes.GetValueOrDefault(prefix, 0);
|
||||
_prefixIndexes[prefix] = index + 1;
|
||||
@@ -415,4 +626,3 @@ public class Generator
|
||||
public required NubType Type { get; init; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user