Skip to content

Commit

Permalink
Prune tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kruhlmann committed Dec 8, 2023
1 parent 3a9c817 commit fe80fcc
Show file tree
Hide file tree
Showing 36 changed files with 109 additions and 529 deletions.
Binary file added mem
Binary file not shown.
12 changes: 12 additions & 0 deletions mem.alc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
include std.sys.mem

[ret:void]
sub main
mem_init
1024 malloc clone printx
mem_walk
1024 malloc clone printx
mem_walk
free
mem_walk
marine
66 changes: 66 additions & 0 deletions mem.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
global _start
segment .text
print:
mov rax, 1
syscall
ret
dump:
mov r9, -3689348814741910323
sub rsp, 40
mov BYTE [rsp+31], 10
lea rcx, [rsp+30]
.dump_loop:
mov rax, rdi
lea r8, [rsp+32]
mul r9
mov rax, rdi
sub r8, rcx
shr rdx, 3
lea rsi, [rdx+rdx*4]
add rsi, rsi
sub rax, rsi
add eax, 48
mov BYTE [rcx], al
mov rax, rdi
mov rdi, rdx
mov rdx, rcx
sub rcx, 1
cmp rax, 9
ja .dump_loop
lea rax, [rsp+32]
mov edi, 1
sub rdx, rax
lea rsi, [rsp+32+rdx]
mov rdx, r8
call print
add rsp, 40
ret
usr_main:

push lit_2
mov rax, 0x1; 1
push rax
mov rax, 0x1; 1
push rax
pop rax
pop rdi
pop rsi
pop rdx
syscall
mov rax, 0x0; 0
push rax
mov rax, 0x3C; 60
push rax
pop rax
pop rdi
syscall
ret
_start:
call usr_main
mov rax, 60
mov rdi, 0
syscall
segment .data
lit_2: db 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00
segment .bss
mem: resb 640000
2 changes: 1 addition & 1 deletion src/compiler/alchemy/amd64/amd64.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CrossReferencer } from "../../../cross_referencer";
import { Instruction } from "../../../instruction";
import { Lexer, AlchemySource } from "../../../lexer";
import { AlchemySource, Lexer } from "../../../lexer";
import { CompilationResult } from "../../result";
import { AlchemyCompiler } from "../alchemy";

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/alchemy/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./amd64";
export * from "./alchemy";
export * from "./amd64";
2 changes: 1 addition & 1 deletion src/compiler/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./alchemy";
export * from "./compiler";
export * from "./nasm";
export * from "./result";
export * from "./alchemy";
11 changes: 5 additions & 6 deletions src/cross_referencer/cross_referencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
WhileInstruction,
} from "../instruction";
import { ArgumentInstruction } from "../instruction/argument_instruction";
import { Logger } from "../logger";
import { ReferenceStackEmptyError } from "./reference_stack_empty_error";
import { ReferenceStackNotEmptyError } from "./reference_stack_not_empty_error";
import { UnexpectedInstructionError } from "./unexpected_instruction_error";
Expand Down Expand Up @@ -56,7 +55,7 @@ export class CrossReferencer {
iclone[curidx] = new CallInstruction(curinst.read_argument());
// Logger.debug(`Converted LiteralInstruction<${curinst.read_argument()}> to ${iclone[curidx]}`);
} else if (iclone[previdx] instanceof UnreferencedSubInstruction) {
iclone[previdx] = new SubInstruction(curinst.read_argument());
iclone[previdx] = new SubInstruction(curinst.read_argument());
// Logger.debug(`Converted LiteralInstruction<${curinst.read_argument()}> to ${iclone[previdx]}`);
} else {
iclone[curidx] = new CallInstruction(curinst.read_argument());
Expand All @@ -81,11 +80,11 @@ export class CrossReferencer {
// Logger.debug(`Linked ${iclone[curidx]} to ${iclone[previdx]}`);
refstack.push(curidx);
} else if (curinst instanceof UnreferencedNextInstruction) {
for (let i = curidx; i > 0; i--) {
if (instructions[i] instanceof WhileInstruction) {
iclone[curidx] = iclone[curidx].reference_to(i);
for (let index = curidx; index > 0; index--) {
if (instructions[index] instanceof WhileInstruction) {
iclone[curidx] = iclone[curidx].reference_to(index);
// Logger.debug(`Linked ${iclone[curidx]} to ${instructions[i]}`);
break
break;
}
}
if (iclone[curidx] instanceof UnreferencedNextInstruction) {
Expand Down
22 changes: 9 additions & 13 deletions src/include/preprocessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "node:fs";
import path from "node:path";
import { Logger } from "../logger";

import { Logger } from "../logger";
import { ModuleNotFoundError } from "./module_not_found_error";

export class IncludePreprocessor {
Expand All @@ -23,16 +23,17 @@ export class IncludePreprocessor {
for (const base_path of this.default_include_paths) {
Logger.debug(`Looking for module ${target_module} in ${base_path}`);
const module_path_candidate = path.join(base_path, module_path);
const is_directory = fs.existsSync(module_path_candidate)
&& fs.lstatSync(module_path_candidate).isDirectory();
const is_file = fs.existsSync(`${module_path_candidate}.alc`)
&& fs.lstatSync(`${module_path_candidate}.alc`).isFile();
const is_directory =
fs.existsSync(module_path_candidate) && fs.lstatSync(module_path_candidate).isDirectory();
const is_file =
fs.existsSync(`${module_path_candidate}.alc`) &&
fs.lstatSync(`${module_path_candidate}.alc`).isFile();

if (is_directory) {
return this.include_directory(module_path_candidate, target_module, included_files);
}
if (is_file) {
return this.include_file(included_files, module_path_candidate)
return this.include_file(included_files, module_path_candidate);
}
}
throw new ModuleNotFoundError(target_module, this.default_include_paths);
Expand All @@ -48,17 +49,12 @@ export class IncludePreprocessor {
return "";
} else {
included_files.push(`${module_path_candidate}.alc`);
return this.resolve_includes(
fs.readFileSync(`${module_path_candidate}.alc`).toString(),
included_files,
);
return this.resolve_includes(fs.readFileSync(`${module_path_candidate}.alc`).toString(), included_files);
}
}

private include_directory(module_path_candidate: string, target_module: string, included_files: string[]) {
const files = fs
.readdirSync(module_path_candidate)
.filter((file) => file.endsWith(".alc"));
const files = fs.readdirSync(module_path_candidate).filter((file) => file.endsWith(".alc"));

if (files.length === 0) {
throw new ModuleNotFoundError(target_module, this.default_include_paths);
Expand Down
2 changes: 1 addition & 1 deletion src/instruction/atomic/poke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Instruction } from "../instruction";

export class PokeInstruction extends Instruction {
public to_asm(_instruction_index: number): string {
return "pop rax\n pop rbx\nadd rsp, rax\nmov [rsp], rbx\nsub rsp, rax"
return "pop rax\n pop rbx\nadd rsp, rax\nmov [rsp], rbx\nsub rsp, rax";
}
public to_wat(_instruction_index: number): string {
throw new Error("Method not implemented.");
Expand Down
4 changes: 1 addition & 3 deletions src/instruction/atomic/push_int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { ArgumentInstruction } from "../argument_instruction";

export class PushIntInstruction extends ArgumentInstruction<BigInt> {
public to_asm(_instruction_index: number): string {
const hex_number = this.argument
.toString(16)
.toUpperCase();
const hex_number = this.argument.toString(16).toUpperCase();
return `mov rax, 0x${hex_number}; ${this.argument.toString()}\npush rax`;
}
public to_wat(_instruction_index: number): string {
Expand Down
6 changes: 3 additions & 3 deletions src/instruction/atomic/push_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export class PushStringInstruction extends ArgumentInstruction<string> {
return `lit_${instruction_index}: db 0x00`;
}
const byte_array = new TextEncoder().encode(this.argument);
const byte_hex = [...byte_array] // Convert implicityly from Uint8Array to
// Array<number> as Uint8Array has a non-standard
// .map implementation.
const byte_hex = [...byte_array] // Convert implicityly from Uint8Array to
// Array<number> as Uint8Array has a non-standard
// .map implementation.
.map((byte) => byte.toString(16))
.map((byte_string) => `0x${byte_string}`)
.join(", ");
Expand Down
4 changes: 2 additions & 2 deletions src/lexer/alchemy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Instruction } from "../instruction";
import { AlchemySource } from "./alchemy_source";
import { Lexer } from "./lexer";
import { LineBuffer } from "./line_buffer";
import { NewlineEscapedString } from "./newline_escaped_string";
import { Symbol } from "./symbol";
import { AlchemySource } from "./alchemy_source";

// eslint-disable-next-line quotes
const DOUBLE_QUOTE = `"`;
Expand Down Expand Up @@ -77,7 +77,7 @@ export class AlchemyLexer implements Lexer<AlchemySource, Instruction[]> {

public lex(source: AlchemySource): Instruction[] {
return source.text
.replace(/\[.*?\]/g, '')
.replace(/\[.*?]/g, "")
.split("\n")
.flatMap((line, line_number) => this.lex_line(line, line_number, source.context));
}
Expand Down
4 changes: 2 additions & 2 deletions src/lexer/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from "./alchemy";
export * from "./alchemy_source";
export * from "./lexer";
export * from "./line_buffer";
export * from "./lookup_table";
export * from "./newline_escaped_string";
export * from "./refmodel";
export * from "./alchemy";
export * from "./alchemy_source";
export * from "./unknown_instruction_error";
4 changes: 2 additions & 2 deletions src/lexer/lookup_table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export class InstructionLookupTable {
">": GreaterThanInstruction,
">=": GreaterThanEqualsInstruction,
">>": RightBinaryShiftInstruction,
"next": UnreferencedNextInstruction,
"return": ReturnInstruction,
next: UnreferencedNextInstruction,
return: ReturnInstruction,
"|": BitwiseOrInstruction,
clone: CloneInstruction,
do: UnreferencedDoInstruction,
Expand Down
2 changes: 1 addition & 1 deletion src/lexer/symbol/int_literal_resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Instruction, PushIntInstruction } from "../../instruction";
import { Logger } from "../../logger";
import { SymbolResolver } from "./resolver";
import { Symbol } from "./symbol";

Expand All @@ -9,6 +8,7 @@ export class IntegerLiteralSymbolResolver implements SymbolResolver {
const parsed = BigInt(symbol.toString());
return new PushIntInstruction(parsed);
} catch {
throw new Error(`Invalid integer ${symbol}`);
}
}
}
33 changes: 0 additions & 33 deletions tests/alchemy_programs/arithmetic/program.refmodel

This file was deleted.

19 changes: 0 additions & 19 deletions tests/alchemy_programs/bitwise/program.refmodel

This file was deleted.

35 changes: 0 additions & 35 deletions tests/alchemy_programs/conditional/program.refmodel

This file was deleted.

Loading

0 comments on commit fe80fcc

Please sign in to comment.