string -> ^u8

This commit is contained in:
nub31
2025-06-07 20:53:17 +02:00
parent 50718ae4ec
commit d7b2aafa27
6 changed files with 28 additions and 61 deletions

View File

@@ -1,26 +1,14 @@
namespace c
extern func printf(fmt: string, ...args: any)
extern func printf(fmt: ^u8, ...args: any)
extern func getchar(): i32
extern func puts(str: string)
extern func putchar(c: i32): i32
extern func fgets(str: ^u8, size: i32, stream: ^any): ^u8
extern func fputs(str: ^u8, stream: ^any): i32
extern func fopen(filename: ^u8, mode: ^u8): ^any
extern func fclose(stream: ^any): i32
extern func puts(fmt: ^u8)
extern func malloc(size: i64): ^any
extern func calloc(num: i64, size: i64): ^any
extern func realloc(ptr: ^any, size: i64): ^any
extern func free(ptr: ^any)
extern func strlen(str: ^u8): i64
extern func strcpy(dest: ^u8, src: ^u8): ^u8
extern func strncpy(dest: ^u8, src: ^u8, n: i64): ^u8
extern func strcat(dest: ^u8, src: ^u8): ^u8
extern func strcmp(s1: ^u8, s2: ^u8): i32
extern func strstr(haystack: ^u8, needle: ^u8): ^u8
extern func sin(x: f64): f64
extern func cos(x: f64): f64
extern func tan(x: f64): f64

View File

@@ -8,10 +8,10 @@ export func main(args: []^string) {
let x = [3]f64
x[0] = 1
x[1.2] = 2
x[1] = 2
x[2] = 3
c::printf("%d\n", x[0])
c::printf("%d\n", x[1])
c::printf("%d\n", x[2])
c::printf("%f\n", x[0])
c::printf("%f\n", x[1])
c::printf("%f\n", x[2])
}

View File

@@ -69,22 +69,13 @@ public static class ConsoleColors
case IdentifierToken:
return White;
case LiteralToken literal:
if (literal.Kind.Equals(NubPrimitiveType.String))
return literal.Kind switch
{
return Green;
}
if (literal.Kind.Equals(NubPrimitiveType.I64) || literal.Kind.Equals(NubPrimitiveType.F64))
{
return BrightBlue;
}
if (literal.Kind.Equals(NubPrimitiveType.Bool))
{
return Blue;
}
return White;
LiteralKind.String => Green,
LiteralKind.Integer or LiteralKind.Float => BrightBlue,
LiteralKind.Bool => Blue,
_ => White
};
case ModifierToken:
return White;
case SymbolToken symbol:

View File

@@ -111,7 +111,6 @@ public class Generator
PrimitiveTypeKind.F64 => "d",
PrimitiveTypeKind.F32 => "s",
PrimitiveTypeKind.Bool => "w",
PrimitiveTypeKind.String => "l",
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in function definitions"),
_ => throw new ArgumentOutOfRangeException()
},
@@ -136,7 +135,6 @@ public class Generator
PrimitiveTypeKind.F64 => "d",
PrimitiveTypeKind.F32 => "s",
PrimitiveTypeKind.Bool => "w",
PrimitiveTypeKind.String => "l",
PrimitiveTypeKind.Any => "l",
_ => throw new ArgumentOutOfRangeException()
},
@@ -167,7 +165,6 @@ public class Generator
PrimitiveTypeKind.F64 => "d",
PrimitiveTypeKind.F32 => "s",
PrimitiveTypeKind.Bool => "w",
PrimitiveTypeKind.String => "l",
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in store instructions"),
_ => throw new ArgumentOutOfRangeException()
},
@@ -196,7 +193,6 @@ public class Generator
PrimitiveTypeKind.F64 => "d",
PrimitiveTypeKind.F32 => "s",
PrimitiveTypeKind.Bool => "w",
PrimitiveTypeKind.String => "l",
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in load instructions"),
_ => throw new ArgumentOutOfRangeException()
},
@@ -225,7 +221,6 @@ public class Generator
PrimitiveTypeKind.F64 => "d",
PrimitiveTypeKind.F32 => "s",
PrimitiveTypeKind.Bool => "w",
PrimitiveTypeKind.String => "l",
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in variables"),
_ => throw new ArgumentOutOfRangeException()
},
@@ -245,7 +240,6 @@ public class Generator
{
case PrimitiveTypeKind.I64:
case PrimitiveTypeKind.U64:
case PrimitiveTypeKind.String:
case PrimitiveTypeKind.Any:
return 8;
case PrimitiveTypeKind.I32:
@@ -417,7 +411,6 @@ public class Generator
PrimitiveTypeKind.F64 => "d",
PrimitiveTypeKind.F32 => "s",
PrimitiveTypeKind.Bool => "w",
PrimitiveTypeKind.String => "l",
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in structs"),
_ => throw new ArgumentOutOfRangeException()
},
@@ -824,7 +817,6 @@ public class Generator
{
case NubPointerType:
case NubStructType:
case NubPrimitiveType { Kind: PrimitiveTypeKind.String }:
case NubPrimitiveType { Kind: PrimitiveTypeKind.I64 }:
case NubPrimitiveType { Kind: PrimitiveTypeKind.F64 }:
case NubPrimitiveType { Kind: PrimitiveTypeKind.U64 }:
@@ -868,11 +860,11 @@ public class Generator
return outputName;
}
if (binaryExpression.Left.Type.Equals(NubPrimitiveType.String))
{
_builder.AppendLine($" {outputName} =w call $nub_strcmp(l {left}, l {right})");
return outputName;
}
// if (binaryExpression.Left.Type.Equals(NubPrimitiveType.String))
// {
// _builder.AppendLine($" {outputName} =w call $nub_strcmp(l {left}, l {right})");
// return outputName;
// }
if (binaryExpression.Left.Type.Equals(NubPrimitiveType.Bool))
{
@@ -896,12 +888,12 @@ public class Generator
return outputName;
}
if (binaryExpression.Left.Type.Equals(NubPrimitiveType.String))
{
_builder.AppendLine($" {outputName} =w call $nub_strcmp(l {left}, l {right})");
_builder.AppendLine($" {outputName} =w xor {outputName}, 1");
return outputName;
}
// if (binaryExpression.Left.Type.Equals(NubPrimitiveType.String))
// {
// _builder.AppendLine($" {outputName} =w call $nub_strcmp(l {left}, l {right})");
// _builder.AppendLine($" {outputName} =w xor {outputName}, 1");
// return outputName;
// }
if (binaryExpression.Left.Type.Equals(NubPrimitiveType.Bool))
{

View File

@@ -16,10 +16,10 @@ public abstract class NubType
return true;
}
if (sourceType.Equals(NubPrimitiveType.String) && targetType is NubArrayType arrayType && IsCompatibleWith(NubPrimitiveType.U8, arrayType.ElementType))
{
return true;
}
// if (sourceType.Equals(NubPrimitiveType.String) && targetType is NubArrayType arrayType && IsCompatibleWith(NubPrimitiveType.U8, arrayType.ElementType))
// {
// return true;
// }
return false;
}
@@ -154,7 +154,6 @@ public class NubPrimitiveType(PrimitiveTypeKind kind) : NubType
public static NubPrimitiveType F32 => new(PrimitiveTypeKind.F32);
public static NubPrimitiveType Bool => new(PrimitiveTypeKind.Bool);
public static NubPrimitiveType String => new(PrimitiveTypeKind.String);
public static NubPrimitiveType Any => new(PrimitiveTypeKind.Any);
public static bool TryParse(string s, [NotNullWhen(true)] out PrimitiveTypeKind? kind)
@@ -172,7 +171,6 @@ public class NubPrimitiveType(PrimitiveTypeKind kind) : NubType
"f64" => PrimitiveTypeKind.F64,
"f32" => PrimitiveTypeKind.F32,
"bool" => PrimitiveTypeKind.Bool,
"string" => PrimitiveTypeKind.String,
"any" => PrimitiveTypeKind.Any,
_ => null
};
@@ -208,7 +206,6 @@ public class NubPrimitiveType(PrimitiveTypeKind kind) : NubType
PrimitiveTypeKind.F64 => "f64",
PrimitiveTypeKind.Bool => "bool",
PrimitiveTypeKind.String => "string",
PrimitiveTypeKind.Any => "any",
_ => throw new ArgumentOutOfRangeException(nameof(kind), Kind, null)
};
@@ -228,6 +225,5 @@ public enum PrimitiveTypeKind
F64,
F32,
Bool,
String,
Any
}

View File

@@ -415,7 +415,7 @@ public class TypeChecker
{
LiteralKind.Integer => NubPrimitiveType.I64,
LiteralKind.Float => NubPrimitiveType.F64,
LiteralKind.String => NubPrimitiveType.String,
LiteralKind.String => new NubArrayType(NubPrimitiveType.U8),
LiteralKind.Bool => NubPrimitiveType.Bool,
_ => throw new ArgumentOutOfRangeException()
};