Skip to content

Commit

Permalink
feat: add run and compile commands to stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
airscripts committed Jul 29, 2023
1 parent 714b87d commit 38f580b
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Analscript
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.
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.

## Contents
- [Installation](#installation)
Expand Down
21 changes: 18 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
#!/usr/bin/env node
import help from './lib/help.js';
import { anallify, stringify } from './lib/stdlib.js';
import { HELP, STRINGIFY, ANALLIFY } from './lib/constants.js';

import {
run,
compile,
anallify,
stringify,
} from './lib/stdlib.js';

import {
RUN,
HELP,
COMPILE,
ANALLIFY,
STRINGIFY,
} from './lib/constants.js';

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

switch (command) {
case COMPILE: compile(args); break;
case RUN: output = run(args); break;
case HELP: process.stdout.write(help); break;
case ANALLIFY: output = anallify(args); break;
case STRINGIFY: output = stringify(args); break;
default: process.stdout.write(help); break;
}

process.stdout.write(output);
process.stdout.write(`${output}\n`);
}

cli();
2 changes: 2 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const RUN = 'run';
export const HELP = 'help';
export const COMPILE = 'compile';
export const ANALLIFY = 'anallify';
export const STRINGIFY = 'stringify';
export const ANAL_CHARACTERS = 'πŸ‘πŸ†';
9 changes: 9 additions & 0 deletions lib/dictionary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const ERROR = {
fileNotFound: 'File not found.',
missingArgument: 'Missing argument.',
notString: 'Only strings are accepted',
};

export const SUCCESS = {
compileSuccess: 'Compilation completed successfully.',
};
45 changes: 37 additions & 8 deletions lib/stdlib.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import fs from 'node:fs';

import checker from './utils.js';
import { ERROR, SUCCESS } from './dictionary.js';
import { ANAL_CHARACTERS } from './constants.js';

export function anallify(string) {
if (!checker(string)) {
process.stderr.write('Only strings are accepted!\n');
process.exit(1);
process.stderr.write(`${ERROR.notString}\n`);
return process.exit(1);
}

if (!string) {
process.stderr.write('Missing argument!\n');
process.exit(1);
process.stderr.write(`${ERROR.missingArgument}\n`);
return process.exit(1);
}

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

export function stringify(anal) {
if (!checker(anal)) {
process.stderr.write('Only strings are accepted!\n');
process.exit(1);
process.stderr.write(`${ERROR.notString}\n`);
return process.exit(1);
}

if (!anal) {
process.stderr.write('Missing argument!\n');
process.exit(1);
process.stderr.write(`${ERROR.missingArgument}\n`);
return process.exit(1);
}

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

return string;
}

export function run(file) {
try {
const contents = fs.readFileSync(file, { encoding: 'utf-8' });
return stringify(contents);
} catch (error) {
process.stderr.write(`${ERROR.fileNotFound}\n`);
return process.exit(1);
}
}

export function compile(file) {
try {
const contents = fs.readFileSync(file, { encoding: 'utf-8' });

let filename = file.split('.');
filename = filename.filter((element, index) => index < filename.length - 1);
filename = filename.join('.');

fs.writeFileSync(`${filename}.anal`, anallify(contents), { encoding: 'utf-8' });
process.stdout.write(`${SUCCESS.compileSuccess}`);
} catch (error) {
process.stderr.write(`${ERROR.fileNotFound}\n`);
process.exit(1);
}
}
1 change: 1 addition & 0 deletions tests/script.anal

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions tests/seeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ export const STRINGIFY_INPUT = 'πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†
export const ANALLIFY_INPUT = 'A';
export const ANALLIFY_WRONG_OUTPUT = 'πŸ‘πŸ†';
export const ANALLIFY_CORRECT_OUTPUT = 'πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†πŸ‘πŸ†';

export const ANAL_FILE_LOCATION = 'tests/script.anal';
export const RUN_CORRECT_OUTPUT = 'Welcome to Analscript!';
export const RUN_WRONG_OUTPUT = 'Welcome to Jurassic Park!';
32 changes: 26 additions & 6 deletions tests/stdlib.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import { test, expect } from 'vitest';

import * as seed from './seeds.js';
import { anallify, stringify } from '../lib/stdlib.js';
import {
ANALLIFY_INPUT,
STRINGIFY_INPUT,
RUN_WRONG_OUTPUT,
ANAL_FILE_LOCATION,
RUN_CORRECT_OUTPUT,
ANALLIFY_WRONG_OUTPUT,
STRINGIFY_WRONG_OUTPUT,
ANALLIFY_CORRECT_OUTPUT,
STRINGIFY_CORRECT_OUTPUT,
} from './seeds.js';

import {
run,
anallify,
stringify,
} from '../lib/stdlib.js';

test('Encode string to anal', () => {
expect(anallify(seed.ANALLIFY_INPUT)).toBe(seed.ANALLIFY_CORRECT_OUTPUT);
expect(anallify(seed.ANALLIFY_INPUT)).not.toBe(seed.ANALLIFY_WRONG_OUTPUT);
expect(anallify(ANALLIFY_INPUT)).toBe(ANALLIFY_CORRECT_OUTPUT);
expect(anallify(ANALLIFY_INPUT)).not.toBe(ANALLIFY_WRONG_OUTPUT);
});

test('Decode anal to string', () => {
expect(stringify(seed.STRINGIFY_INPUT)).toBe(seed.STRINGIFY_CORRECT_OUTPUT);
expect(stringify(seed.STRINGIFY_INPUT)).not.toBe(seed.STRINGIFY_WRONG_OUTPUT);
expect(stringify(STRINGIFY_INPUT)).toBe(STRINGIFY_CORRECT_OUTPUT);
expect(stringify(STRINGIFY_INPUT)).not.toBe(STRINGIFY_WRONG_OUTPUT);
});

test('Run .anal file', () => {
expect(run(ANAL_FILE_LOCATION)).toBe(RUN_CORRECT_OUTPUT);
expect(run(ANAL_FILE_LOCATION)).not.toBe(RUN_WRONG_OUTPUT);
});

0 comments on commit 38f580b

Please sign in to comment.