Skip to content

Commit

Permalink
Rename SymbolTypeFlags to SymbolTypeFlag (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron authored Jun 7, 2023
1 parent 57455f6 commit 0df0422
Show file tree
Hide file tree
Showing 27 changed files with 238 additions and 238 deletions.
24 changes: 12 additions & 12 deletions src/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { SignatureHelpUtil } from './bscPlugin/SignatureHelpUtil';
import { DiagnosticSeverityAdjuster } from './DiagnosticSeverityAdjuster';
import { IntegerType } from './types/IntegerType';
import { StringType } from './types/StringType';
import { SymbolTable, SymbolTypeFlags } from './SymbolTable';
import { SymbolTable, SymbolTypeFlag } from './SymbolTable';
import { BooleanType } from './types/BooleanType';
import { DoubleType } from './types/DoubleType';
import { DynamicType } from './types/DynamicType';
Expand Down Expand Up @@ -124,20 +124,20 @@ export class Program {
//Setup primitive types in global symbolTable
//TODO: Need to handle Array types

this.globalScope.symbolTable.addSymbol('boolean', undefined, BooleanType.instance, SymbolTypeFlags.typetime);
this.globalScope.symbolTable.addSymbol('double', undefined, DoubleType.instance, SymbolTypeFlags.typetime);
this.globalScope.symbolTable.addSymbol('dynamic', undefined, DynamicType.instance, SymbolTypeFlags.typetime);
this.globalScope.symbolTable.addSymbol('float', undefined, FloatType.instance, SymbolTypeFlags.typetime);
this.globalScope.symbolTable.addSymbol('function', undefined, new FunctionType(DynamicType.instance), SymbolTypeFlags.typetime);
this.globalScope.symbolTable.addSymbol('integer', undefined, IntegerType.instance, SymbolTypeFlags.typetime);
this.globalScope.symbolTable.addSymbol('longinteger', undefined, LongIntegerType.instance, SymbolTypeFlags.typetime);
this.globalScope.symbolTable.addSymbol('object', undefined, new ObjectType(), SymbolTypeFlags.typetime);
this.globalScope.symbolTable.addSymbol('string', undefined, StringType.instance, SymbolTypeFlags.typetime);
this.globalScope.symbolTable.addSymbol('void', undefined, VoidType.instance, SymbolTypeFlags.typetime);
this.globalScope.symbolTable.addSymbol('boolean', undefined, BooleanType.instance, SymbolTypeFlag.typetime);
this.globalScope.symbolTable.addSymbol('double', undefined, DoubleType.instance, SymbolTypeFlag.typetime);
this.globalScope.symbolTable.addSymbol('dynamic', undefined, DynamicType.instance, SymbolTypeFlag.typetime);
this.globalScope.symbolTable.addSymbol('float', undefined, FloatType.instance, SymbolTypeFlag.typetime);
this.globalScope.symbolTable.addSymbol('function', undefined, new FunctionType(DynamicType.instance), SymbolTypeFlag.typetime);
this.globalScope.symbolTable.addSymbol('integer', undefined, IntegerType.instance, SymbolTypeFlag.typetime);
this.globalScope.symbolTable.addSymbol('longinteger', undefined, LongIntegerType.instance, SymbolTypeFlag.typetime);
this.globalScope.symbolTable.addSymbol('object', undefined, new ObjectType(), SymbolTypeFlag.typetime);
this.globalScope.symbolTable.addSymbol('string', undefined, StringType.instance, SymbolTypeFlag.typetime);
this.globalScope.symbolTable.addSymbol('void', undefined, VoidType.instance, SymbolTypeFlag.typetime);

for (let pair of globalCallableMap) {
let [key, callable] = pair;
this.globalScope.symbolTable.addSymbol(key, undefined, callable.type, SymbolTypeFlags.runtime);
this.globalScope.symbolTable.addSymbol(key, undefined, callable.type, SymbolTypeFlag.runtime);
}
}

Expand Down
54 changes: 27 additions & 27 deletions src/Scope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Logger } from './Logger';
import type { BrsFile } from './files/BrsFile';
import type { FunctionStatement, NamespaceStatement } from './parser/Statement';
import type { OnScopeValidateEvent } from './interfaces';
import { SymbolTypeFlags } from './SymbolTable';
import { SymbolTypeFlag } from './SymbolTable';
import { EnumMemberType } from './types/EnumType';
import { ClassType } from './types/ClassType';
import { BooleanType } from './types/BooleanType';
Expand Down Expand Up @@ -82,15 +82,15 @@ describe('Scope', () => {
//the symbol table should contain the relative names for all items in this namespace across the entire scope
expect(
// eslint-disable-next-line no-bitwise
symbolTable.hasSymbol('Beta', SymbolTypeFlags.runtime | SymbolTypeFlags.typetime)
symbolTable.hasSymbol('Beta', SymbolTypeFlag.runtime | SymbolTypeFlag.typetime)
).to.be.true;
expect(
// eslint-disable-next-line no-bitwise
symbolTable.hasSymbol('Charlie', SymbolTypeFlags.runtime | SymbolTypeFlags.typetime)
symbolTable.hasSymbol('Charlie', SymbolTypeFlag.runtime | SymbolTypeFlag.typetime)
).to.be.true;
expect(
// eslint-disable-next-line no-bitwise
symbolTable.hasSymbol('createBeta', SymbolTypeFlags.runtime)
symbolTable.hasSymbol('createBeta', SymbolTypeFlag.runtime)
).to.be.true;

expectZeroDiagnostics(program);
Expand Down Expand Up @@ -2186,9 +2186,9 @@ describe('Scope', () => {
expect(mainFnScope).to.exist;
sourceScope.linkSymbolTable();
const mainSymbolTable = mainFnScope.symbolTable;
expectTypeToBe(mainSymbolTable.getSymbol('fooInstance', SymbolTypeFlags.runtime)[0].type, ClassType);
expect(mainSymbolTable.getSymbol('fooInstance', SymbolTypeFlags.runtime)[0].type.toString()).to.eq('Foo');
let myNumType = mainSymbolTable.getSymbolType('myNum', { flags: SymbolTypeFlags.runtime });
expectTypeToBe(mainSymbolTable.getSymbol('fooInstance', SymbolTypeFlag.runtime)[0].type, ClassType);
expect(mainSymbolTable.getSymbol('fooInstance', SymbolTypeFlag.runtime)[0].type.toString()).to.eq('Foo');
let myNumType = mainSymbolTable.getSymbolType('myNum', { flags: SymbolTypeFlag.runtime });
expectTypeToBe(myNumType, IntegerType);
});

Expand All @@ -2211,7 +2211,7 @@ describe('Scope', () => {
expect(sourceScope).to.exist;
expect(mainFnScope).to.exist;
sourceScope.linkSymbolTable();
expectTypeToBe(mainFnScope.symbolTable.getSymbol('paintColor', SymbolTypeFlags.runtime)[0].type, EnumMemberType);
expectTypeToBe(mainFnScope.symbolTable.getSymbol('paintColor', SymbolTypeFlag.runtime)[0].type, EnumMemberType);
});

it('finds correct class field type with default value enums are used', () => {
Expand Down Expand Up @@ -2239,7 +2239,7 @@ describe('Scope', () => {
expect(mainFnScope).to.exist;
//sourceScope.linkSymbolTable();
let mainScopeSymbolTable = mainFnScope.symbolTable;
let paintType = mainScopeSymbolTable.getSymbolType('paintColor', { flags: SymbolTypeFlags.runtime });
let paintType = mainScopeSymbolTable.getSymbolType('paintColor', { flags: SymbolTypeFlag.runtime });
expectTypeToBe(paintType, EnumMemberType);
});

Expand All @@ -2254,15 +2254,15 @@ describe('Scope', () => {
expect(sourceScope).to.exist;
sourceScope.linkSymbolTable();
expect(mainFnScope).to.exist;
expectTypeToBe(mainFnScope.symbolTable.getSymbol('skin', SymbolTypeFlags.runtime)[0].type, EnumMemberType);

expectTypeToBe(mainFnScope.symbolTable.getSymbol('flyBoy', SymbolTypeFlags.runtime)[0].type, ClassType);
expect(mainFnScope.symbolTable.getSymbol('flyBoy', SymbolTypeFlags.runtime)[0].type.toString()).to.eq('Animals.Bird');
expectTypeToBe(mainFnScope.symbolTable.getSymbol('flyBoysWings', SymbolTypeFlags.runtime)[0].type, BooleanType);
expectTypeToBe(mainFnScope.symbolTable.getSymbol('flyBoysSkin', SymbolTypeFlags.runtime)[0].type, EnumMemberType);
expectTypeToBe(mainFnScope.symbolTable.getSymbol('fido', SymbolTypeFlags.runtime)[0].type, ClassType);
expect(mainFnScope.symbolTable.getSymbol('fido', SymbolTypeFlags.runtime)[0].type.toString()).to.eq('Animals.Dog');
expectTypeToBe(mainFnScope.symbolTable.getSymbol('fidoBark', SymbolTypeFlags.runtime)[0].type, StringType);
expectTypeToBe(mainFnScope.symbolTable.getSymbol('skin', SymbolTypeFlag.runtime)[0].type, EnumMemberType);

expectTypeToBe(mainFnScope.symbolTable.getSymbol('flyBoy', SymbolTypeFlag.runtime)[0].type, ClassType);
expect(mainFnScope.symbolTable.getSymbol('flyBoy', SymbolTypeFlag.runtime)[0].type.toString()).to.eq('Animals.Bird');
expectTypeToBe(mainFnScope.symbolTable.getSymbol('flyBoysWings', SymbolTypeFlag.runtime)[0].type, BooleanType);
expectTypeToBe(mainFnScope.symbolTable.getSymbol('flyBoysSkin', SymbolTypeFlag.runtime)[0].type, EnumMemberType);
expectTypeToBe(mainFnScope.symbolTable.getSymbol('fido', SymbolTypeFlag.runtime)[0].type, ClassType);
expect(mainFnScope.symbolTable.getSymbol('fido', SymbolTypeFlag.runtime)[0].type.toString()).to.eq('Animals.Dog');
expectTypeToBe(mainFnScope.symbolTable.getSymbol('fidoBark', SymbolTypeFlag.runtime)[0].type, StringType);
});

it('finds correct type for members of classes with super classes', () => {
Expand All @@ -2275,12 +2275,12 @@ describe('Scope', () => {
expect(sourceScope).to.exist;
sourceScope.linkSymbolTable();
expect(mainFnScope).to.exist;
const chimpType = mainFnScope.symbolTable.getSymbol('chimp', SymbolTypeFlags.runtime)[0].type;
const chimpType = mainFnScope.symbolTable.getSymbol('chimp', SymbolTypeFlag.runtime)[0].type;
expectTypeToBe(chimpType, ClassType);
expectTypeToBe((chimpType as ClassType).superClass, ClassType);
expectTypeToBe(mainFnScope.symbolTable.getSymbol('chimpHasLegs', SymbolTypeFlags.runtime)[0].type, BooleanType);
expectTypeToBe(mainFnScope.symbolTable.getSymbol('chimpSpeed', SymbolTypeFlags.runtime)[0].type, IntegerType);
expectTypeToBe(mainFnScope.symbolTable.getSymbol('fidoSpeed', SymbolTypeFlags.runtime)[0].type, IntegerType);
expectTypeToBe(mainFnScope.symbolTable.getSymbol('chimpHasLegs', SymbolTypeFlag.runtime)[0].type, BooleanType);
expectTypeToBe(mainFnScope.symbolTable.getSymbol('chimpSpeed', SymbolTypeFlag.runtime)[0].type, IntegerType);
expectTypeToBe(mainFnScope.symbolTable.getSymbol('fidoSpeed', SymbolTypeFlag.runtime)[0].type, IntegerType);
});

it('finds correct types for method calls', () => {
Expand Down Expand Up @@ -2308,7 +2308,7 @@ describe('Scope', () => {
expect(sourceScope).to.exist;
sourceScope.linkSymbolTable();
expect(mainFnScope).to.exist;
expectTypeToBe(mainFnScope.symbolTable.getSymbol('myVal', SymbolTypeFlags.runtime)[0].type, IntegerType);
expectTypeToBe(mainFnScope.symbolTable.getSymbol('myVal', SymbolTypeFlag.runtime)[0].type, IntegerType);
});

it('finds correct types for self-referencing variables', () => {
Expand All @@ -2326,7 +2326,7 @@ describe('Scope', () => {
expect(sourceScope).to.exist;
sourceScope.linkSymbolTable();
expect(mainFnScope).to.exist;
const getTypeOptions = { flags: SymbolTypeFlags.runtime };
const getTypeOptions = { flags: SymbolTypeFlag.runtime };
let dtType = mainFnScope.symbolTable.getSymbolType('dt', getTypeOptions);
expectTypeToBe(dtType, ObjectType);
let hoursType = mainFnScope.symbolTable.getSymbolType('hours', getTypeOptions);
Expand Down Expand Up @@ -2360,7 +2360,7 @@ describe('Scope', () => {
sourceScope.linkSymbolTable();
expect(mainFnScope).to.exist;
const mainSymbolTable = mainFnScope.symbolTable;
expectTypeToBe(mainSymbolTable.getSymbolType('name', { flags: SymbolTypeFlags.runtime }), StringType);
expectTypeToBe(mainSymbolTable.getSymbolType('name', { flags: SymbolTypeFlag.runtime }), StringType);
});

it('should have an error when a non union member is accessed', () => {
Expand Down Expand Up @@ -2405,7 +2405,7 @@ describe('Scope', () => {
const sourceScope = program.getScopeByName('source');
sourceScope.linkSymbolTable();
let mainSymbolTable = mainFnScope.symbolTable;
expectTypeToBe(mainSymbolTable.getSymbol('value', SymbolTypeFlags.runtime)[0].type, FloatType);
expectTypeToBe(mainSymbolTable.getSymbol('value', SymbolTypeFlag.runtime)[0].type, FloatType);
});


Expand All @@ -2425,7 +2425,7 @@ describe('Scope', () => {
const sourceScope = program.getScopeByName('source');
sourceScope.linkSymbolTable();
let mainSymbolTable = mainFnScope.symbolTable;
expectTypeToBe(mainSymbolTable.getSymbol('value', SymbolTypeFlags.runtime)[0].type, StringType);
expectTypeToBe(mainSymbolTable.getSymbol('value', SymbolTypeFlag.runtime)[0].type, StringType);
});
});
});
Expand Down
62 changes: 31 additions & 31 deletions src/SymbolTable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from './chai-config.spec';
import { StringType } from './types/StringType';
import { IntegerType } from './types/IntegerType';
import { BooleanType } from './types/BooleanType';
import { SymbolTypeFlags } from './SymbolTable';
import { SymbolTypeFlag } from './SymbolTable';

describe('SymbolTable', () => {
let parent: SymbolTable;
Expand All @@ -14,76 +14,76 @@ describe('SymbolTable', () => {

it('is case insensitive', () => {
const st = new SymbolTable('Child');
st.addSymbol('foo', null, new StringType(), SymbolTypeFlags.runtime);
expect(st.getSymbol('FOO', SymbolTypeFlags.runtime).length).eq(1);
expect(st.getSymbol('FOO', SymbolTypeFlags.runtime)[0].type.toString()).eq('string');
st.addSymbol('foo', null, new StringType(), SymbolTypeFlag.runtime);
expect(st.getSymbol('FOO', SymbolTypeFlag.runtime).length).eq(1);
expect(st.getSymbol('FOO', SymbolTypeFlag.runtime)[0].type.toString()).eq('string');
});

it('stores all previous symbols', () => {
const st = new SymbolTable('Child');
st.addSymbol('foo', null, new StringType(), SymbolTypeFlags.runtime);
st.addSymbol('foo', null, new IntegerType(), SymbolTypeFlags.runtime);
expect(st.getSymbol('FOO', SymbolTypeFlags.runtime).length).eq(2);
st.addSymbol('foo', null, new StringType(), SymbolTypeFlag.runtime);
st.addSymbol('foo', null, new IntegerType(), SymbolTypeFlag.runtime);
expect(st.getSymbol('FOO', SymbolTypeFlag.runtime).length).eq(2);
});


it('reads from parent symbol table if not found in current', () => {
const st = new SymbolTable('Child', () => parent);
parent.addSymbol('foo', null, new StringType(), SymbolTypeFlags.runtime);
expect(st.getSymbol('foo', SymbolTypeFlags.runtime)[0].type.toString()).eq('string');
parent.addSymbol('foo', null, new StringType(), SymbolTypeFlag.runtime);
expect(st.getSymbol('foo', SymbolTypeFlag.runtime)[0].type.toString()).eq('string');
});

it('reads from current table if it exists', () => {
const st = new SymbolTable('Child', () => parent);
parent.addSymbol('foo', null, new StringType(), SymbolTypeFlags.runtime);
st.addSymbol('foo', null, new IntegerType(), SymbolTypeFlags.runtime);
expect(st.getSymbol('foo', SymbolTypeFlags.runtime)[0].type.toString()).eq('integer');
parent.addSymbol('foo', null, new StringType(), SymbolTypeFlag.runtime);
st.addSymbol('foo', null, new IntegerType(), SymbolTypeFlag.runtime);
expect(st.getSymbol('foo', SymbolTypeFlag.runtime)[0].type.toString()).eq('integer');
});

it('correct checks if a symbol is in the table using hasSymbol', () => {
const child = new SymbolTable('Child', () => parent);
parent.addSymbol('foo', null, new StringType(), SymbolTypeFlags.runtime);
child.addSymbol('bar', null, new IntegerType(), SymbolTypeFlags.runtime);
expect(parent.hasSymbol('foo', SymbolTypeFlags.runtime)).to.be.true;
expect(parent.hasSymbol('bar', SymbolTypeFlags.runtime)).to.be.false;
expect(child.hasSymbol('foo', SymbolTypeFlags.runtime)).to.be.true;
expect(child.hasSymbol('bar', SymbolTypeFlags.runtime)).to.be.true;
expect(child.hasSymbol('buz', SymbolTypeFlags.runtime)).to.be.false;
parent.addSymbol('foo', null, new StringType(), SymbolTypeFlag.runtime);
child.addSymbol('bar', null, new IntegerType(), SymbolTypeFlag.runtime);
expect(parent.hasSymbol('foo', SymbolTypeFlag.runtime)).to.be.true;
expect(parent.hasSymbol('bar', SymbolTypeFlag.runtime)).to.be.false;
expect(child.hasSymbol('foo', SymbolTypeFlag.runtime)).to.be.true;
expect(child.hasSymbol('bar', SymbolTypeFlag.runtime)).to.be.true;
expect(child.hasSymbol('buz', SymbolTypeFlag.runtime)).to.be.false;
});

it('matches bitflags given', () => {
const table = new SymbolTable('Child', () => parent);
table.addSymbol('foo', null, new StringType(), SymbolTypeFlags.runtime);
table.addSymbol('bar', null, new IntegerType(), SymbolTypeFlags.typetime);
expect(table.hasSymbol('foo', SymbolTypeFlags.runtime)).to.be.true;
expect(table.hasSymbol('bar', SymbolTypeFlags.runtime)).to.be.false;
expect(table.hasSymbol('foo', SymbolTypeFlags.typetime)).to.be.false;
expect(table.hasSymbol('bar', SymbolTypeFlags.typetime)).to.be.true;
table.addSymbol('foo', null, new StringType(), SymbolTypeFlag.runtime);
table.addSymbol('bar', null, new IntegerType(), SymbolTypeFlag.typetime);
expect(table.hasSymbol('foo', SymbolTypeFlag.runtime)).to.be.true;
expect(table.hasSymbol('bar', SymbolTypeFlag.runtime)).to.be.false;
expect(table.hasSymbol('foo', SymbolTypeFlag.typetime)).to.be.false;
expect(table.hasSymbol('bar', SymbolTypeFlag.typetime)).to.be.true;
});

describe('mergeSymbolTable', () => {

it('adds each symbol to the table', () => {
const st = new SymbolTable('Child');
st.addSymbol('foo', null, new StringType(), SymbolTypeFlags.runtime);
st.addSymbol('foo', null, new StringType(), SymbolTypeFlag.runtime);
const otherTable = new SymbolTable('OtherTable');
otherTable.addSymbol('bar', null, new IntegerType(), SymbolTypeFlags.runtime);
otherTable.addSymbol('foo', null, new IntegerType(), SymbolTypeFlags.runtime);
otherTable.addSymbol('bar', null, new IntegerType(), SymbolTypeFlag.runtime);
otherTable.addSymbol('foo', null, new IntegerType(), SymbolTypeFlag.runtime);
st.mergeSymbolTable(otherTable);
});
});

it('searches siblings before parents', () => {
parent.addSymbol('alpha', null, new StringType(), SymbolTypeFlags.runtime);
parent.addSymbol('alpha', null, new StringType(), SymbolTypeFlag.runtime);

const child = new SymbolTable('Child', () => parent);

const sibling = new SymbolTable('Sibling');
child.addSibling(sibling);
sibling.addSymbol('alpha', null, new BooleanType(), SymbolTypeFlags.runtime);
sibling.addSymbol('alpha', null, new BooleanType(), SymbolTypeFlag.runtime);

expect(
child.getSymbol('alpha', SymbolTypeFlags.runtime).map(x => x.type.toTypeString())
child.getSymbol('alpha', SymbolTypeFlag.runtime).map(x => x.type.toTypeString())
).to.eql([
'boolean'
]);
Expand Down
Loading

0 comments on commit 0df0422

Please sign in to comment.