Add I32, mirroring Wasm i32

This commit is contained in:
nora 2023-07-30 14:53:43 +02:00
parent 89dbb50add
commit 50e82066c9
5 changed files with 28 additions and 3 deletions

View file

@ -115,7 +115,7 @@ export type ExprBreak = {
* The break target block. * The break target block.
* May be any control flow block, labelled from inside out. * May be any control flow block, labelled from inside out.
*/ */
target?: number, target?: number;
}; };
export type ExprStructLiteral = { export type ExprStructLiteral = {
@ -261,6 +261,7 @@ export const BUILTINS = [
"print", "print",
"String", "String",
"Int", "Int",
"I32",
"Bool", "Bool",
"true", "true",
"false", "false",
@ -282,7 +283,10 @@ export type TyString = {
export type TyInt = { export type TyInt = {
kind: "int"; kind: "int";
signed: boolean; };
export type TyI32 = {
kind: "i32";
}; };
export type TyBool = { export type TyBool = {
@ -328,6 +332,7 @@ export type TyNever = {
export type Ty = export type Ty =
| TyString | TyString
| TyInt | TyInt
| TyI32
| TyBool | TyBool
| TyList | TyList
| TyTuple | TyTuple
@ -343,7 +348,8 @@ export function tyIsUnit(ty: Ty): ty is TyUnit {
export const TY_UNIT: Ty = { kind: "tuple", elems: [] }; export const TY_UNIT: Ty = { kind: "tuple", elems: [] };
export const TY_STRING: Ty = { kind: "string" }; export const TY_STRING: Ty = { kind: "string" };
export const TY_BOOL: Ty = { kind: "bool" }; export const TY_BOOL: Ty = { kind: "bool" };
export const TY_INT: Ty = { kind: "int", signed: false }; export const TY_INT: Ty = { kind: "int" };
export const TY_I32: Ty = { kind: "i32" };
export const TY_NEVER: Ty = { kind: "never" }; export const TY_NEVER: Ty = { kind: "never" };
export type TypeckResults = { export type TypeckResults = {

View file

@ -10,11 +10,15 @@ import fs from "fs";
import { exec } from "child_process"; import { exec } from "child_process";
const input = ` const input = `
// import "wasi_snapshot_preview1" "fd_write"(a: I32, b: I32, c: I32, d: I32): I32;
function main() = ( function main() = (
loop (no(break);); loop (no(break););
uwu(10); uwu(10);
); );
function meow(a: I32): I32 = a;
function no(a: !): String = a; function no(a: !): String = a;
function uwu(a: Int) = if a != 0 then ( function uwu(a: Int) = if a != 0 then (

View file

@ -524,6 +524,8 @@ function computeAbi(ty: TyFn): FnAbi {
todo("fn abi"); todo("fn abi");
case "int": case "int":
return ["i64"]; return ["i64"];
case "i32":
return ["i32"];
case "bool": case "bool":
return ["i32"]; return ["i32"];
case "list": case "list":
@ -572,6 +574,8 @@ function wasmTypeForBody(ty: Ty): wasm.ValType[] {
return STRING_TYPES; return STRING_TYPES;
case "int": case "int":
return ["i64"]; return ["i64"];
case "i32":
return ["i32"];
case "bool": case "bool":
return ["i32"]; return ["i32"];
case "list": case "list":

View file

@ -178,6 +178,9 @@ export function printTy(ty: Ty): string {
case "int": { case "int": {
return "Int"; return "Int";
} }
case "i32": {
return "I32";
}
case "bool": { case "bool": {
return "Bool"; return "Bool";
} }

View file

@ -15,6 +15,7 @@ import {
Resolution, Resolution,
Ty, Ty,
TY_BOOL, TY_BOOL,
TY_I32,
TY_INT, TY_INT,
TY_NEVER, TY_NEVER,
TY_STRING, TY_STRING,
@ -34,6 +35,9 @@ function builtinAsTy(name: string, span: Span): Ty {
case "Int": { case "Int": {
return TY_INT; return TY_INT;
} }
case "I32": {
return TY_I32;
}
case "Bool": { case "Bool": {
return TY_BOOL; return TY_BOOL;
} }
@ -355,6 +359,10 @@ export class InferContext {
if (rhs.kind === "int") return; if (rhs.kind === "int") return;
break; break;
} }
case "i32": {
if (rhs.kind === "i32") return;
break;
}
case "bool": { case "bool": {
if (rhs.kind === "bool") return; if (rhs.kind === "bool") return;
break; break;