Skip to content

Commit

Permalink
Merge pull request #189 from uqbar-project/printer
Browse files Browse the repository at this point in the history
Primera version printer
  • Loading branch information
PalumboN authored Nov 26, 2023
2 parents 1ef6c62 + 9d81c00 commit 37dc198
Show file tree
Hide file tree
Showing 12 changed files with 3,252 additions and 56 deletions.
89 changes: 87 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
"test:sanity": "npm run test:wtest -- --root language/test/sanity",
"test:validations": "mocha --parallel -r ts-node/register/transpile-only test/validator.test.ts",
"test:wtest": "mocha --delay -t 10000 -r ts-node/register/transpile-only test/wtest.ts",
"test:printer": "mocha --parallel -r ts-node/register/transpile-only test/printer.test.ts",
"test:parser": "mocha --parallel -r ts-node/register/transpile-only test/parser.test.ts",
"prepublishOnly": "npm run build && npm test",
"postpublish": "git tag v$npm_package_version && git push --tags",
"prepack": "npm run build"
},
"dependencies": {
"@types/parsimmon": "^1.10.8",
"parsimmon": "^1.18.1",
"prettier-printer": "^1.1.4",
"unraw": "^3.0.0",
"uuid": "^9.0.1"
},
Expand All @@ -44,6 +47,7 @@
"@typescript-eslint/parser": "^5.53.0",
"chai": "^4.3.7",
"chalk": "^5.2.0",
"dedent": "^1.5.1",
"eslint": "^8.35.0",
"globby": "^11.1.0",
"mocha": "^10.2.0",
Expand Down
61 changes: 60 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,62 @@
import { Name } from './model'

export const WOLLOK_EXTRA_STACK_TRACE_HEADER = 'Derived from TypeScript stack'

export const WOLLOK_BASE_PACKAGE = 'wollok.'
export const WOLLOK_BASE_PACKAGE = 'wollok.'

export const PREFIX_OPERATORS: Record<Name, Name> = {
'!': 'negate',
'-': 'invert',
'+': 'plus',
'not': 'negate',
}

export const ASSIGNATION_OPERATORS = ['=', '||=', '/=', '-=', '+=', '*=', '&&=', '%=']

export const INFIX_OPERATORS = [
['||', 'or'],
['&&', 'and'],
['===', '==', '!==', '!='],
['>=', '>', '<=', '<'],
['?:', '>>>', '>>', '>..', '<>', '<=>', '<<<', '<<', '..<', '..', '->'],
['-', '+'],
['/', '*'],
['**', '%'],
]

export const LIST_MODULE= 'wollok.lang.List'
export const SET_MODULE= 'wollok.lang.Set'
export const OBJECT_MODULE= 'wollok.lang.Object'

export const KEYWORDS = {
IF: 'if',
ELSE: 'else',
NEW: 'new',
SELF: 'self',
SUPER: 'super',
NULL: 'null',
METHOD: 'method',
VAR: 'var',
THROW: 'throw',
TRY: 'try',
CATCH: 'catch',
THEN: 'then',
ALWAYS: 'always',
PROPERTY: 'property',
CONST: 'const',
OVERRIDE: 'override',
NATIVE: 'native',
RETURN: 'return',
IMPORT: 'import',
SUITE: 'describe',
TEST: 'test',
MIXIN: 'mixin',
CLASS: 'class',
INHERITS: 'inherits',
DERIVED: 'derived',
MIXED_AND: 'and',
WKO: 'object',
FIXTURE: 'fixture',
PROGRAM: 'program',
PACKAGE: 'package',
} as const
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { List } from './extensions'
import { fromJSON } from './jsonUtils'
import * as parse from './parser'
import validate from './validator'
import print from './printer/print'
import WRE from './wre/wre.json'
import WRENatives from './wre/wre.natives'

Expand All @@ -29,4 +30,5 @@ export {
parse,
link,
validate,
print,
}
2 changes: 1 addition & 1 deletion src/interpreter/runtimeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ export class Evaluation {
const target = node.instantiated.target ?? raise(new Error(`Could not resolve reference to instantiated module ${node.instantiated.name}`))

const name = node.instantiated.name

if (!target.is(Class)) raise(new Error(`${name} is not a class, you cannot generate instances of a ${target?.kind}`))

if (target.isAbstract) raise(new Error(`${name} is an abstract class, you cannot generate instances`))
Expand Down
20 changes: 12 additions & 8 deletions src/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cached, getPotentiallyUninitializedLazy, lazy } from './decorators'
import { ConstructorFor, InstanceOf, is, last, List, mapObject, Mixable, MixinDefinition, MIXINS, notEmpty, TypeDefinition } from './extensions'
import { ConstructorFor, InstanceOf, List, MIXINS, Mixable, MixinDefinition, TypeDefinition, is, last, mapObject, notEmpty } from './extensions'

const { isArray } = Array
const { entries, values, assign } = Object
Expand Down Expand Up @@ -471,6 +471,16 @@ export function Module<S extends Mixable<Node>>(supertype: S) {
return undefined
}

@cached
get isAbstract(): boolean {
return this.abstractMethods.some(method => !this.lookupMethod(method.name, method.parameters.length))
}

@cached
get abstractMethods(): List<Method> {
return this.hierarchy.flatMap(module => module.methods.filter(method => method.isAbstract()))
}

@cached
defaultValueFor(field: Field): Expression {
if(!this.allFields.includes(field)) throw new Error('Field does not belong to the module')
Expand Down Expand Up @@ -508,12 +518,6 @@ export class Class extends Module(Node) {
return this === objectClass ? undefined : objectClass
}
}

@cached
get isAbstract(): boolean {
const abstractMethods = this.hierarchy.flatMap(module => module.methods.filter(method => method.isAbstract()))
return abstractMethods.some(method => !this.lookupMethod(method.name, method.parameters.length))
}
}


Expand Down Expand Up @@ -750,7 +754,7 @@ export class Super extends Expression(Node) {

export class New extends Expression(Node) {
get kind(): 'New' { return 'New' }
readonly instantiated!: Reference<Class>
readonly instantiated!: Reference<Module>
readonly args!: List<NamedArgument>

constructor({ args = [], ...payload }: Payload<New, 'instantiated'>) {
Expand Down
Loading

0 comments on commit 37dc198

Please sign in to comment.