Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
array testing
Browse files Browse the repository at this point in the history
  • Loading branch information
eoussama committed Mar 22, 2024
1 parent 66ad633 commit b25c8a8
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/core/enums/errno.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export enum Errno {
InvalidFileError,
UnknownFileError,
MissingColonError,
InvalidArrayError,
InvalidStringError,
MissingEqualsError,
ExpectedCommaError,
UnclosedStringError,
Expand Down
2 changes: 2 additions & 0 deletions src/core/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ export * from './missing-dot.error';
export * from './unknown-file.error';
export * from './expected-key.error';
export * from './invalid-file.error';
export * from './invalid-array.error';
export * from './missing-colon.error';
export * from './invalid-string.error';
export * from './expected-comma.error';
export * from './missing-equals.error';
export * from './unclosed-string.error';
Expand Down
13 changes: 13 additions & 0 deletions src/core/errors/invalid-array.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Errno } from '../enums/errno.enum';
import { LangKamaError } from './langkama.error';



export class InvalidArrayError extends LangKamaError {
constructor() {
super('identifier is not a valid array');

this.name = 'InvalidArrayError';
this.errno = Errno.InvalidArrayError;
}
}
13 changes: 13 additions & 0 deletions src/core/errors/invalid-string.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Errno } from '../enums/errno.enum';
import { LangKamaError } from './langkama.error';



export class InvalidStringError extends LangKamaError {
constructor() {
super('name is not a string');

this.name = 'InvalidStringError';
this.errno = Errno.InvalidStringError;
}
}
12 changes: 6 additions & 6 deletions src/runtime/environment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConstantReassignmentError, TOnStdOutCallbackFn, Type, VariableDefinedError, VariableNotDefinedError } from '..';
import { ConstantReassignmentError, InvalidArrayError, InvalidStringError, TOnStdOutCallbackFn, Type, VariableDefinedError, VariableNotDefinedError } from '..';

import { ErrorManager } from '../core/managers/error.manager';
import { RuntimeHelper } from '../core/helpers/runtime.helper';
Expand Down Expand Up @@ -74,7 +74,7 @@ export class Environment {
const [arr] = args;

if (arr.type !== Type.Array) {
throw 'needs to be an array';
this.errorManager?.raise(new InvalidArrayError());
}

return RuntimeHelper.createNumber((arr as IArrayVal).value.length);
Expand All @@ -84,13 +84,13 @@ export class Environment {
const [arrName, value] = args;

if (arrName.type !== Type.String) {
throw 'needs to ne a string';
this.errorManager?.raise(new InvalidStringError());
}

const arrayVal = this.getValue((arrName as IStringVal).value).value as IArrayVal;

if (arrayVal.type !== Type.Array) {
throw 'needs to be an array';
this.errorManager?.raise(new InvalidArrayError());
}

const newArray = [...arrayVal.value];
Expand All @@ -104,13 +104,13 @@ export class Environment {
const [arrName] = args;

if (arrName.type !== Type.String) {
throw 'needs to ne a string';
this.errorManager?.raise(new InvalidStringError());
}

const arrayVal = this.getValue((arrName as IStringVal).value).value as IArrayVal;

if (arrayVal.type !== Type.Array) {
throw 'needs to be an array';
this.errorManager?.raise(new InvalidArrayError());
}

const newArray = [...arrayVal.value];
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@ export class Evaluator {
const index = this.evaluate(array.index, env) as INumberVal;
const identifier = this.evaluate(array.identifier, env) as IArrayVal;
const arr = (identifier as IArrayVal).value;
const value = arr[index.value];

return RuntimeHelper.createValue(arr[index.value]);
return value ? RuntimeHelper.createValue(value) : RuntimeHelper.createNull();
}

/**
Expand Down
225 changes: 225 additions & 0 deletions test/arrays.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
const Errno = require('../dist/@nakamaorg/langkama.umd.cjs').Errno;
const LangKama = require('../dist/@nakamaorg/langkama.umd.cjs').LangKama;
const LangKamaEvent = require('../dist/@nakamaorg/langkama.umd.cjs').LangKamaEvent;



describe('Arrays', () => {
let compiler;

beforeEach(() => {
compiler = new LangKama();
});

test('Array declaration with value 1', done => {
const code = `
a sa7 hear me out arr[] is [];
reda arr;
`;

compiler
.on(LangKamaEvent.Success, result => {
expect(result.value).toEqual([]);
done();
})
.interpret(code);
});

test('Array declaration with value 2', done => {
const code = `
a sa7 hear me out arr[] is [1, 2, 3];
reda arr;
`;

compiler
.on(LangKamaEvent.Success, result => {
expect(result.value).toEqual([1, 2, 3]);
done();
})
.interpret(code);
});

test('Array declaration without value', done => {
const code = `
hear me out arr[];
reda arr;
`;

compiler
.on(LangKamaEvent.Success, result => {
expect(result.value).toEqual([]);
done();
})
.interpret(code);
});

test('Array indexing inbound', done => {
const code = `
a sa7 hear me out arr[] is [1, 2, 3];
reda arr[1];
`;

compiler
.on(LangKamaEvent.Success, result => {
expect(result.value).toBe(2);
done();
})
.interpret(code);
});

test('Array indexing out of bound', done => {
const code = `
a sa7 hear me out arr[] is [1, 2, 3];
reda arr[3];
`;

compiler
.on(LangKamaEvent.Success, result => {
expect(result.value).toBe(null);
done();
})
.interpret(code);
});

test('Array index assignment', done => {
const code = `
hear me out arr[] is [1, 2, 3];
arr[0] is 100;
reda arr;
`;

compiler
.on(LangKamaEvent.Success, result => {
expect(result.value).toEqual([100, 2, 3]);
done();
})
.interpret(code);
});

test('Array length', done => {
const code = `
hear me out arr[] is [1, 2, 3];
reda length(arr);
`;

compiler
.on(LangKamaEvent.Success, result => {
expect(result.value).toBe(3);
done();
})
.interpret(code);
});

test('Array push 1', done => {
const code = `
hear me out arr[];
push("arr", 0);
push("arr", 1);
push("arr", 2);
reda arr;
`;

compiler
.on(LangKamaEvent.Success, result => {
expect(result.value).toEqual([0, 1, 2]);
done();
})
.interpret(code);
});

test('Array push 2', done => {
const code = `
hear me out arr[];
hear me out i is 0;
cook until (i = 100) {
big if true ((i % 2) = 0) {
push("arr", i);
}
i is i + 1;
}
reda push("arr", 100);
`;

compiler
.on(LangKamaEvent.Success, result => {
expect(result.value).toBe(51);
done();
})
.interpret(code);
});

test('Array pop 1', done => {
const code = `
hear me out arr[] is [1, 2, 3];
pop("arr");
pop("arr");
reda arr;
`;

compiler
.on(LangKamaEvent.Success, result => {
expect(result.value).toEqual([1]);
done();
})
.interpret(code);
});

test('Array pop 2', done => {
const code = `
hear me out arr[];
hear me out i is 0;
cook until (i = 100) {
push("arr", i);
i is i + 1;
}
i is 0;
cook until (i = 100) {
big if true ((i % 2) = 0) {
pop("arr");
}
i is i + 1;
}
reda pop("arr");
`;

compiler
.on(LangKamaEvent.Success, result => {
expect(result.value).toBe(49);
done();
})
.interpret(code);
});
});

describe('Arrays errors', () => {
let compiler;

beforeEach(() => {
compiler = new LangKama();
});

test('Missing close bracket', done => {
const code = `
hear me out arr[ is [];
`;

compiler
.on(LangKamaEvent.Error, result => {
expect(result.errno).toBe(Errno.ExpectedCloseBrackError);
done();
})
.interpret(code);
});
});

0 comments on commit b25c8a8

Please sign in to comment.