Remove number and add int and float insted

This commit is contained in:
Fabian Stamm
2022-04-13 19:01:12 +00:00
parent 339d3006d6
commit b980af4b17
10 changed files with 64 additions and 41 deletions

View File

@ -1,5 +1,5 @@
import * as FS from "fs";
import * as FSE from "fs-extra"
import * as FSE from "fs-extra";
import * as Path from "path";
import {
EnumDefinition,
@ -11,7 +11,10 @@ import {
export abstract class CompileTarget<T = any> {
abstract name: string;
constructor(private outputFolder: string, protected options: T & { use_messagepack: boolean }) {
constructor(
private outputFolder: string,
protected options: T & { allow_bytes: boolean }
) {
if (!FS.existsSync(outputFolder)) {
FS.mkdirSync(outputFolder, {
recursive: true,
@ -30,12 +33,13 @@ export abstract class CompileTarget<T = any> {
abstract finalize(steps: Step[]): void;
protected writeFile(name: string, content: string | Promise<string>) {
let resPath = Path.join(this.outputFolder, name);
let resDir = Path.dirname(resPath);
if (!FS.existsSync(resDir)) FS.mkdirSync(resDir, { recursive: true });
if (content instanceof Promise) {
content.then((res) =>
FS.writeFileSync(Path.join(this.outputFolder, name), res)
);
content.then((res) => FS.writeFileSync(resPath, res));
} else {
FS.writeFileSync(Path.join(this.outputFolder, name), content);
FS.writeFileSync(resPath, content);
}
}
@ -59,12 +63,10 @@ export abstract class CompileTarget<T = any> {
return res.join("\n");
}
protected loadTemplateFolder(name:string) {
protected loadTemplateFolder(name: string) {
let root = Path.join(__dirname, "../templates/", name);
FSE.copySync(root, this.outputFolder, {
});
FSE.copySync(root, this.outputFolder, {});
}
}

View File

@ -2,7 +2,7 @@ import type { Parsed, StatementNode } from "./parser";
import dbg from "debug";
const log = dbg("app");
const BUILTIN = ["number", "string", "boolean"];
const BUILTIN = ["float", "int", "string", "boolean"];
export class IRError extends Error {
constructor(public statement: StatementNode, message: string) {
@ -115,7 +115,7 @@ export default function get_ir(parsed: Parsed): IR {
depends.push(field.fieldtype);
}
if (field.map && field.map !== "number" && field.map !== "string") {
if (field.map && field.map !== "int" && field.map !== "string") {
throw new IRError(
field,
`Type ${field.map} is not valid as map key!`
@ -312,6 +312,7 @@ export default function get_ir(parsed: Parsed): IR {
statement.key == "allow_bytes") &&
statement.value == "true"
) {
options["allow_bytes"] = true;
builtin.push("bytes");
}
} else {

View File

@ -22,7 +22,6 @@ Targets.set("ts-esm", ESMTypescriptTarget);
Targets.set("ts-node", NodeJSTypescriptTarget);
Targets.set("c#", CSharpTarget as typeof CompileTarget);
function indexToLineAndCol(src: string, index: number) {
let line = 1;
let col = 1;

View File

@ -12,10 +12,11 @@ type lineAppender = (ind: number, line: string | string[]) => void;
const conversion = {
boolean: "bool",
number: "double",
int: "long",
float: "double",
string: "string",
void: "void",
bytes: ""
bytes: "",
};
function toCSharpType(type: string): string {
@ -30,8 +31,8 @@ export class CSharpTarget extends CompileTarget<{ csharp_namespace: string }> {
}
start(): void {
if(this.options.use_messagepack == true) {
throw new Error("C# has no support for MessagePack yet!");
if (this.options.allow_bytes == true) {
throw new Error("C# has no support for 'bytes' yet!");
}
this.writeFile(
this.namespace + ".csproj",

View File

@ -13,7 +13,8 @@ type lineAppender = (ind: number, line: string | string[]) => void;
const conversion = {
boolean: "boolean",
number: "number",
int: "number",
float: "number",
string: "string",
void: "void",
bytes: "Uint8Array",
@ -45,7 +46,7 @@ export class TypescriptTarget extends CompileTarget {
a(
0,
this.generateImport(
`{ VerificationError, apply_number, apply_string, apply_boolean, apply_void }`,
`{ VerificationError, apply_int, apply_float, apply_string, apply_boolean, apply_void }`,
`./ts_base`
)
);