string -> ^u8
This commit is contained in:
@@ -1,26 +1,14 @@
|
|||||||
namespace c
|
namespace c
|
||||||
|
|
||||||
extern func printf(fmt: string, ...args: any)
|
extern func printf(fmt: ^u8, ...args: any)
|
||||||
extern func getchar(): i32
|
extern func getchar(): i32
|
||||||
extern func puts(str: string)
|
extern func puts(fmt: ^u8)
|
||||||
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 malloc(size: i64): ^any
|
extern func malloc(size: i64): ^any
|
||||||
extern func calloc(num: i64, size: i64): ^any
|
extern func calloc(num: i64, size: i64): ^any
|
||||||
extern func realloc(ptr: ^any, size: i64): ^any
|
extern func realloc(ptr: ^any, size: i64): ^any
|
||||||
extern func free(ptr: ^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 sin(x: f64): f64
|
||||||
extern func cos(x: f64): f64
|
extern func cos(x: f64): f64
|
||||||
extern func tan(x: f64): f64
|
extern func tan(x: f64): f64
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ export func main(args: []^string) {
|
|||||||
let x = [3]f64
|
let x = [3]f64
|
||||||
|
|
||||||
x[0] = 1
|
x[0] = 1
|
||||||
x[1.2] = 2
|
x[1] = 2
|
||||||
x[2] = 3
|
x[2] = 3
|
||||||
|
|
||||||
c::printf("%d\n", x[0])
|
c::printf("%f\n", x[0])
|
||||||
c::printf("%d\n", x[1])
|
c::printf("%f\n", x[1])
|
||||||
c::printf("%d\n", x[2])
|
c::printf("%f\n", x[2])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,22 +69,13 @@ public static class ConsoleColors
|
|||||||
case IdentifierToken:
|
case IdentifierToken:
|
||||||
return White;
|
return White;
|
||||||
case LiteralToken literal:
|
case LiteralToken literal:
|
||||||
if (literal.Kind.Equals(NubPrimitiveType.String))
|
return literal.Kind switch
|
||||||
{
|
{
|
||||||
return Green;
|
LiteralKind.String => Green,
|
||||||
}
|
LiteralKind.Integer or LiteralKind.Float => BrightBlue,
|
||||||
|
LiteralKind.Bool => Blue,
|
||||||
if (literal.Kind.Equals(NubPrimitiveType.I64) || literal.Kind.Equals(NubPrimitiveType.F64))
|
_ => White
|
||||||
{
|
};
|
||||||
return BrightBlue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (literal.Kind.Equals(NubPrimitiveType.Bool))
|
|
||||||
{
|
|
||||||
return Blue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return White;
|
|
||||||
case ModifierToken:
|
case ModifierToken:
|
||||||
return White;
|
return White;
|
||||||
case SymbolToken symbol:
|
case SymbolToken symbol:
|
||||||
|
|||||||
@@ -111,7 +111,6 @@ public class Generator
|
|||||||
PrimitiveTypeKind.F64 => "d",
|
PrimitiveTypeKind.F64 => "d",
|
||||||
PrimitiveTypeKind.F32 => "s",
|
PrimitiveTypeKind.F32 => "s",
|
||||||
PrimitiveTypeKind.Bool => "w",
|
PrimitiveTypeKind.Bool => "w",
|
||||||
PrimitiveTypeKind.String => "l",
|
|
||||||
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in function definitions"),
|
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in function definitions"),
|
||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
@@ -136,7 +135,6 @@ public class Generator
|
|||||||
PrimitiveTypeKind.F64 => "d",
|
PrimitiveTypeKind.F64 => "d",
|
||||||
PrimitiveTypeKind.F32 => "s",
|
PrimitiveTypeKind.F32 => "s",
|
||||||
PrimitiveTypeKind.Bool => "w",
|
PrimitiveTypeKind.Bool => "w",
|
||||||
PrimitiveTypeKind.String => "l",
|
|
||||||
PrimitiveTypeKind.Any => "l",
|
PrimitiveTypeKind.Any => "l",
|
||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
@@ -167,7 +165,6 @@ public class Generator
|
|||||||
PrimitiveTypeKind.F64 => "d",
|
PrimitiveTypeKind.F64 => "d",
|
||||||
PrimitiveTypeKind.F32 => "s",
|
PrimitiveTypeKind.F32 => "s",
|
||||||
PrimitiveTypeKind.Bool => "w",
|
PrimitiveTypeKind.Bool => "w",
|
||||||
PrimitiveTypeKind.String => "l",
|
|
||||||
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in store instructions"),
|
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in store instructions"),
|
||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
@@ -196,7 +193,6 @@ public class Generator
|
|||||||
PrimitiveTypeKind.F64 => "d",
|
PrimitiveTypeKind.F64 => "d",
|
||||||
PrimitiveTypeKind.F32 => "s",
|
PrimitiveTypeKind.F32 => "s",
|
||||||
PrimitiveTypeKind.Bool => "w",
|
PrimitiveTypeKind.Bool => "w",
|
||||||
PrimitiveTypeKind.String => "l",
|
|
||||||
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in load instructions"),
|
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in load instructions"),
|
||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
@@ -225,7 +221,6 @@ public class Generator
|
|||||||
PrimitiveTypeKind.F64 => "d",
|
PrimitiveTypeKind.F64 => "d",
|
||||||
PrimitiveTypeKind.F32 => "s",
|
PrimitiveTypeKind.F32 => "s",
|
||||||
PrimitiveTypeKind.Bool => "w",
|
PrimitiveTypeKind.Bool => "w",
|
||||||
PrimitiveTypeKind.String => "l",
|
|
||||||
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in variables"),
|
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in variables"),
|
||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
@@ -245,7 +240,6 @@ public class Generator
|
|||||||
{
|
{
|
||||||
case PrimitiveTypeKind.I64:
|
case PrimitiveTypeKind.I64:
|
||||||
case PrimitiveTypeKind.U64:
|
case PrimitiveTypeKind.U64:
|
||||||
case PrimitiveTypeKind.String:
|
|
||||||
case PrimitiveTypeKind.Any:
|
case PrimitiveTypeKind.Any:
|
||||||
return 8;
|
return 8;
|
||||||
case PrimitiveTypeKind.I32:
|
case PrimitiveTypeKind.I32:
|
||||||
@@ -417,7 +411,6 @@ public class Generator
|
|||||||
PrimitiveTypeKind.F64 => "d",
|
PrimitiveTypeKind.F64 => "d",
|
||||||
PrimitiveTypeKind.F32 => "s",
|
PrimitiveTypeKind.F32 => "s",
|
||||||
PrimitiveTypeKind.Bool => "w",
|
PrimitiveTypeKind.Bool => "w",
|
||||||
PrimitiveTypeKind.String => "l",
|
|
||||||
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in structs"),
|
PrimitiveTypeKind.Any => throw new NotSupportedException("any type cannot be used in structs"),
|
||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
},
|
},
|
||||||
@@ -824,7 +817,6 @@ public class Generator
|
|||||||
{
|
{
|
||||||
case NubPointerType:
|
case NubPointerType:
|
||||||
case NubStructType:
|
case NubStructType:
|
||||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.String }:
|
|
||||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.I64 }:
|
case NubPrimitiveType { Kind: PrimitiveTypeKind.I64 }:
|
||||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.F64 }:
|
case NubPrimitiveType { Kind: PrimitiveTypeKind.F64 }:
|
||||||
case NubPrimitiveType { Kind: PrimitiveTypeKind.U64 }:
|
case NubPrimitiveType { Kind: PrimitiveTypeKind.U64 }:
|
||||||
@@ -868,11 +860,11 @@ public class Generator
|
|||||||
return outputName;
|
return outputName;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (binaryExpression.Left.Type.Equals(NubPrimitiveType.String))
|
// if (binaryExpression.Left.Type.Equals(NubPrimitiveType.String))
|
||||||
{
|
// {
|
||||||
_builder.AppendLine($" {outputName} =w call $nub_strcmp(l {left}, l {right})");
|
// _builder.AppendLine($" {outputName} =w call $nub_strcmp(l {left}, l {right})");
|
||||||
return outputName;
|
// return outputName;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (binaryExpression.Left.Type.Equals(NubPrimitiveType.Bool))
|
if (binaryExpression.Left.Type.Equals(NubPrimitiveType.Bool))
|
||||||
{
|
{
|
||||||
@@ -896,12 +888,12 @@ public class Generator
|
|||||||
return outputName;
|
return outputName;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (binaryExpression.Left.Type.Equals(NubPrimitiveType.String))
|
// if (binaryExpression.Left.Type.Equals(NubPrimitiveType.String))
|
||||||
{
|
// {
|
||||||
_builder.AppendLine($" {outputName} =w call $nub_strcmp(l {left}, l {right})");
|
// _builder.AppendLine($" {outputName} =w call $nub_strcmp(l {left}, l {right})");
|
||||||
_builder.AppendLine($" {outputName} =w xor {outputName}, 1");
|
// _builder.AppendLine($" {outputName} =w xor {outputName}, 1");
|
||||||
return outputName;
|
// return outputName;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (binaryExpression.Left.Type.Equals(NubPrimitiveType.Bool))
|
if (binaryExpression.Left.Type.Equals(NubPrimitiveType.Bool))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ public abstract class NubType
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sourceType.Equals(NubPrimitiveType.String) && targetType is NubArrayType arrayType && IsCompatibleWith(NubPrimitiveType.U8, arrayType.ElementType))
|
// if (sourceType.Equals(NubPrimitiveType.String) && targetType is NubArrayType arrayType && IsCompatibleWith(NubPrimitiveType.U8, arrayType.ElementType))
|
||||||
{
|
// {
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -154,7 +154,6 @@ public class NubPrimitiveType(PrimitiveTypeKind kind) : NubType
|
|||||||
public static NubPrimitiveType F32 => new(PrimitiveTypeKind.F32);
|
public static NubPrimitiveType F32 => new(PrimitiveTypeKind.F32);
|
||||||
|
|
||||||
public static NubPrimitiveType Bool => new(PrimitiveTypeKind.Bool);
|
public static NubPrimitiveType Bool => new(PrimitiveTypeKind.Bool);
|
||||||
public static NubPrimitiveType String => new(PrimitiveTypeKind.String);
|
|
||||||
public static NubPrimitiveType Any => new(PrimitiveTypeKind.Any);
|
public static NubPrimitiveType Any => new(PrimitiveTypeKind.Any);
|
||||||
|
|
||||||
public static bool TryParse(string s, [NotNullWhen(true)] out PrimitiveTypeKind? kind)
|
public static bool TryParse(string s, [NotNullWhen(true)] out PrimitiveTypeKind? kind)
|
||||||
@@ -172,7 +171,6 @@ public class NubPrimitiveType(PrimitiveTypeKind kind) : NubType
|
|||||||
"f64" => PrimitiveTypeKind.F64,
|
"f64" => PrimitiveTypeKind.F64,
|
||||||
"f32" => PrimitiveTypeKind.F32,
|
"f32" => PrimitiveTypeKind.F32,
|
||||||
"bool" => PrimitiveTypeKind.Bool,
|
"bool" => PrimitiveTypeKind.Bool,
|
||||||
"string" => PrimitiveTypeKind.String,
|
|
||||||
"any" => PrimitiveTypeKind.Any,
|
"any" => PrimitiveTypeKind.Any,
|
||||||
_ => null
|
_ => null
|
||||||
};
|
};
|
||||||
@@ -208,7 +206,6 @@ public class NubPrimitiveType(PrimitiveTypeKind kind) : NubType
|
|||||||
PrimitiveTypeKind.F64 => "f64",
|
PrimitiveTypeKind.F64 => "f64",
|
||||||
|
|
||||||
PrimitiveTypeKind.Bool => "bool",
|
PrimitiveTypeKind.Bool => "bool",
|
||||||
PrimitiveTypeKind.String => "string",
|
|
||||||
PrimitiveTypeKind.Any => "any",
|
PrimitiveTypeKind.Any => "any",
|
||||||
_ => throw new ArgumentOutOfRangeException(nameof(kind), Kind, null)
|
_ => throw new ArgumentOutOfRangeException(nameof(kind), Kind, null)
|
||||||
};
|
};
|
||||||
@@ -228,6 +225,5 @@ public enum PrimitiveTypeKind
|
|||||||
F64,
|
F64,
|
||||||
F32,
|
F32,
|
||||||
Bool,
|
Bool,
|
||||||
String,
|
|
||||||
Any
|
Any
|
||||||
}
|
}
|
||||||
@@ -415,7 +415,7 @@ public class TypeChecker
|
|||||||
{
|
{
|
||||||
LiteralKind.Integer => NubPrimitiveType.I64,
|
LiteralKind.Integer => NubPrimitiveType.I64,
|
||||||
LiteralKind.Float => NubPrimitiveType.F64,
|
LiteralKind.Float => NubPrimitiveType.F64,
|
||||||
LiteralKind.String => NubPrimitiveType.String,
|
LiteralKind.String => new NubArrayType(NubPrimitiveType.U8),
|
||||||
LiteralKind.Bool => NubPrimitiveType.Bool,
|
LiteralKind.Bool => NubPrimitiveType.Bool,
|
||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => throw new ArgumentOutOfRangeException()
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user