Skip to content

Commit 38f580b

Browse files
committed
feat: add run and compile commands to stdlib
1 parent 714b87d commit 38f580b

File tree

8 files changed

+98
-18
lines changed

8 files changed

+98
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Analscript
2-
Analscript is a joke esoteric programming language, that takes inspiration from the fallen Anal Lang and serves, the purpose to be a modern approach for writing anally fast stuff.
2+
Analscript is an esoteric programming language, that takes inspiration from the fallen Anal Lang and serves, the purpose to be a modern approach for writing anally fast stuff.
33

44
## Contents
55
- [Installation](#installation)

cli.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
#!/usr/bin/env node
22
import help from './lib/help.js';
3-
import { anallify, stringify } from './lib/stdlib.js';
4-
import { HELP, STRINGIFY, ANALLIFY } from './lib/constants.js';
3+
4+
import {
5+
run,
6+
compile,
7+
anallify,
8+
stringify,
9+
} from './lib/stdlib.js';
10+
11+
import {
12+
RUN,
13+
HELP,
14+
COMPILE,
15+
ANALLIFY,
16+
STRINGIFY,
17+
} from './lib/constants.js';
518

619
function cli() {
720
let output = '';
821
const command = process.argv[2];
922
const args = process.argv.slice(3, process.argv.length).join('');
1023

1124
switch (command) {
25+
case COMPILE: compile(args); break;
26+
case RUN: output = run(args); break;
1227
case HELP: process.stdout.write(help); break;
1328
case ANALLIFY: output = anallify(args); break;
1429
case STRINGIFY: output = stringify(args); break;
1530
default: process.stdout.write(help); break;
1631
}
1732

18-
process.stdout.write(output);
33+
process.stdout.write(`${output}\n`);
1934
}
2035

2136
cli();

lib/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
export const RUN = 'run';
12
export const HELP = 'help';
3+
export const COMPILE = 'compile';
24
export const ANALLIFY = 'anallify';
35
export const STRINGIFY = 'stringify';
46
export const ANAL_CHARACTERS = '🍑🍆';

lib/dictionary.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const ERROR = {
2+
fileNotFound: 'File not found.',
3+
missingArgument: 'Missing argument.',
4+
notString: 'Only strings are accepted',
5+
};
6+
7+
export const SUCCESS = {
8+
compileSuccess: 'Compilation completed successfully.',
9+
};

lib/stdlib.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
import fs from 'node:fs';
2+
13
import checker from './utils.js';
4+
import { ERROR, SUCCESS } from './dictionary.js';
25
import { ANAL_CHARACTERS } from './constants.js';
36

47
export function anallify(string) {
58
if (!checker(string)) {
6-
process.stderr.write('Only strings are accepted!\n');
7-
process.exit(1);
9+
process.stderr.write(`${ERROR.notString}\n`);
10+
return process.exit(1);
811
}
912

1013
if (!string) {
11-
process.stderr.write('Missing argument!\n');
12-
process.exit(1);
14+
process.stderr.write(`${ERROR.missingArgument}\n`);
15+
return process.exit(1);
1316
}
1417

1518
let anal = '';
@@ -26,13 +29,13 @@ export function anallify(string) {
2629

2730
export function stringify(anal) {
2831
if (!checker(anal)) {
29-
process.stderr.write('Only strings are accepted!\n');
30-
process.exit(1);
32+
process.stderr.write(`${ERROR.notString}\n`);
33+
return process.exit(1);
3134
}
3235

3336
if (!anal) {
34-
process.stderr.write('Missing argument!\n');
35-
process.exit(1);
37+
process.stderr.write(`${ERROR.missingArgument}\n`);
38+
return process.exit(1);
3639
}
3740

3841
let string = '';
@@ -47,3 +50,29 @@ export function stringify(anal) {
4750

4851
return string;
4952
}
53+
54+
export function run(file) {
55+
try {
56+
const contents = fs.readFileSync(file, { encoding: 'utf-8' });
57+
return stringify(contents);
58+
} catch (error) {
59+
process.stderr.write(`${ERROR.fileNotFound}\n`);
60+
return process.exit(1);
61+
}
62+
}
63+
64+
export function compile(file) {
65+
try {
66+
const contents = fs.readFileSync(file, { encoding: 'utf-8' });
67+
68+
let filename = file.split('.');
69+
filename = filename.filter((element, index) => index < filename.length - 1);
70+
filename = filename.join('.');
71+
72+
fs.writeFileSync(`${filename}.anal`, anallify(contents), { encoding: 'utf-8' });
73+
process.stdout.write(`${SUCCESS.compileSuccess}`);
74+
} catch (error) {
75+
process.stderr.write(`${ERROR.fileNotFound}\n`);
76+
process.exit(1);
77+
}
78+
}

tests/script.anal

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

tests/seeds.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ export const STRINGIFY_INPUT = '🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆
55
export const ANALLIFY_INPUT = 'A';
66
export const ANALLIFY_WRONG_OUTPUT = '🍑🍆';
77
export const ANALLIFY_CORRECT_OUTPUT = '🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆';
8+
9+
export const ANAL_FILE_LOCATION = 'tests/script.anal';
10+
export const RUN_CORRECT_OUTPUT = 'Welcome to Analscript!';
11+
export const RUN_WRONG_OUTPUT = 'Welcome to Jurassic Park!';

tests/stdlib.test.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
import { test, expect } from 'vitest';
22

3-
import * as seed from './seeds.js';
4-
import { anallify, stringify } from '../lib/stdlib.js';
3+
import {
4+
ANALLIFY_INPUT,
5+
STRINGIFY_INPUT,
6+
RUN_WRONG_OUTPUT,
7+
ANAL_FILE_LOCATION,
8+
RUN_CORRECT_OUTPUT,
9+
ANALLIFY_WRONG_OUTPUT,
10+
STRINGIFY_WRONG_OUTPUT,
11+
ANALLIFY_CORRECT_OUTPUT,
12+
STRINGIFY_CORRECT_OUTPUT,
13+
} from './seeds.js';
14+
15+
import {
16+
run,
17+
anallify,
18+
stringify,
19+
} from '../lib/stdlib.js';
520

621
test('Encode string to anal', () => {
7-
expect(anallify(seed.ANALLIFY_INPUT)).toBe(seed.ANALLIFY_CORRECT_OUTPUT);
8-
expect(anallify(seed.ANALLIFY_INPUT)).not.toBe(seed.ANALLIFY_WRONG_OUTPUT);
22+
expect(anallify(ANALLIFY_INPUT)).toBe(ANALLIFY_CORRECT_OUTPUT);
23+
expect(anallify(ANALLIFY_INPUT)).not.toBe(ANALLIFY_WRONG_OUTPUT);
924
});
1025

1126
test('Decode anal to string', () => {
12-
expect(stringify(seed.STRINGIFY_INPUT)).toBe(seed.STRINGIFY_CORRECT_OUTPUT);
13-
expect(stringify(seed.STRINGIFY_INPUT)).not.toBe(seed.STRINGIFY_WRONG_OUTPUT);
27+
expect(stringify(STRINGIFY_INPUT)).toBe(STRINGIFY_CORRECT_OUTPUT);
28+
expect(stringify(STRINGIFY_INPUT)).not.toBe(STRINGIFY_WRONG_OUTPUT);
29+
});
30+
31+
test('Run .anal file', () => {
32+
expect(run(ANAL_FILE_LOCATION)).toBe(RUN_CORRECT_OUTPUT);
33+
expect(run(ANAL_FILE_LOCATION)).not.toBe(RUN_WRONG_OUTPUT);
1434
});

0 commit comments

Comments
 (0)