Skip to content

Commit

Permalink
Make compiler runnable
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Aug 3, 2024
1 parent 9b3f1a8 commit c07c715
Show file tree
Hide file tree
Showing 16 changed files with 31 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist
pnpm-lock.yaml
0x
tmp
data/test.no
1 change: 0 additions & 1 deletion src/ast/ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { tokenize } from '../lexer/lexer'
import { Parser } from '../parser'
import { parseModule } from '../parser/fns'
import { Context } from '../scope'
import { vidFromString } from '../scope/util'
import { Module, buildModuleAst, compactAstNode } from './index'

describe('ast', () => {
Expand Down
2 changes: 0 additions & 2 deletions src/ast/statement.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ParseNode, filterNonAstNodes } from '../parser'
import { Context } from '../scope'
import { InstanceRelation } from '../scope/trait'
import { assert } from '../util/todo'
import { Expr, buildExpr } from './expr'
import { BaseAstNode, Param, buildParam } from './index'
Expand Down Expand Up @@ -150,7 +149,6 @@ export type ImplDef = BaseAstNode & {
forTrait?: Identifier
block: Block
superMethods?: CallOp[]
rel?: InstanceRelation
}

export const buildImplDef = (node: ParseNode, ctx: Context): ImplDef => {
Expand Down
3 changes: 0 additions & 3 deletions src/codegen/js/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Module } from '../../ast'
import { Context } from '../../scope'
import { concatVid, vidFromString } from '../../scope/util'
import { VirtualIdentifier } from '../../scope/vid'
import { Upcast } from '../../semantic/upcast'
import { VirtualType, virtualTypeToString } from '../../typecheck'
import { groupBy } from '../../util/array'
import { unreachable } from '../../util/todo'
import { EmitNode, EmitToken, emitToken, emitTree } from './node'
Expand Down
2 changes: 0 additions & 2 deletions src/codegen/js/native.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { extractValue, jsRelName, nextVariable } from '.'
import { Context } from '../../scope'
import { string, trace } from '../../scope/std'
import { InstanceRelation, resolveTypeImpl } from '../../scope/trait'
import { resolveVid } from '../../scope/vid'
import { todo } from '../../util/todo'
import { EmitNode, emitToken, emitTree, jsVariable } from './node'

Expand Down
3 changes: 0 additions & 3 deletions src/codegen/js/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { Block, BreakStmt, FnDef, ImplDef, ReturnStmt, Statement, TraitDef, VarD
import { TypeDef, Variant } from '../../ast/type-def'
import { Context } from '../../scope'
import { trace } from '../../scope/std'
import { typeDefToVirtualType } from '../../scope/trait'
import { vidEq, vidToString } from '../../scope/util'
import { virtualTypeToString } from '../../typecheck'
import { EmitExpr, emitExpr, emitParam, emitPattern } from './expr'
import { emitTraceImpl } from './native'
import { EmitNode, emitIntersperse, emitToken, emitTree, jsVariable } from './node'
Expand Down
2 changes: 0 additions & 2 deletions src/scope/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { Config } from '../config'
import { Package } from '../package'
import { SemanticError } from '../semantic/error'
import { unreachable } from '../util/todo'
import { vidToString } from './util'
import { VirtualIdentifier } from './vid'

export type Context = {
config: Config
Expand Down
37 changes: 12 additions & 25 deletions src/scope/std.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
import { VidType } from '../typecheck'
import { holeType, unknownType } from '../typecheck/type'
import { vidFromString } from './util'
import { Identifier } from '../ast/operand'
import { idFromString } from '../typecheck'

export const preludeVid = vidFromString('std::prelude')
export const preludeVid = idFromString('std::prelude')

// TODO: lack of std types must throw notFoundError
export const bool: VidType = { kind: 'vid-type', identifier: vidFromString('std::bool::Bool'), typeArgs: [] }
export const string: VidType = { kind: 'vid-type', identifier: vidFromString('std::string::String'), typeArgs: [] }
export const list: VidType = { kind: 'vid-type', identifier: vidFromString('std::list::List'), typeArgs: [holeType] }
export const bool: Identifier = idFromString('std::bool::Bool')
export const string: Identifier = idFromString('std::string::String')
export const list: Identifier = idFromString('std::list::List')

export const show: VidType = { kind: 'vid-type', identifier: vidFromString('std::io::show::Show'), typeArgs: [] }
export const trace: VidType = { kind: 'vid-type', identifier: vidFromString('std::io::trace::Trace'), typeArgs: [] }
export const show: Identifier = idFromString('std::io::show::Show')
export const trace: Identifier = idFromString('std::io::trace::Trace')

export const iter: VidType = { kind: 'vid-type', identifier: vidFromString('std::iter::Iter'), typeArgs: [unknownType] }
export const iterable: VidType = {
kind: 'vid-type',
identifier: vidFromString('std::iter::Iterable'),
typeArgs: [holeType]
}
export const unwrap: VidType = {
kind: 'vid-type',
identifier: vidFromString('std::unwrap::Unwrap'),
typeArgs: [holeType]
}
export const future: VidType = {
kind: 'vid-type',
identifier: vidFromString('std::future::Future'),
typeArgs: [holeType]
}
export const iter: Identifier = idFromString('std::iter::Iter')
export const iterable: Identifier = idFromString('std::iter::Iterable')
export const unwrap: Identifier = idFromString('std::unwrap::Unwrap')
export const future: Identifier = idFromString('std::future::Future')
2 changes: 0 additions & 2 deletions src/semantic/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import { FnDef, ImplDef, Statement, VarDef } from '../ast/statement'
import { Type } from '../ast/type'
import { FieldDef } from '../ast/type-def'
import { Context } from '../scope'
import { vidToString } from '../scope/util'
import { Source } from '../source'
import { VirtualFnType, VirtualType, virtualTypeToString } from '../typecheck'
import { assert, unreachable } from '../util/todo'
import { MatchTree, unmatchedPaths } from './exhaust'

Expand Down
4 changes: 1 addition & 3 deletions src/semantic/exhaust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import { MatchExpr, PatternExpr } from '../ast/match'
import { Variant } from '../ast/type-def'
import { Context, addError, addWarning } from '../scope'
import { concatVid, idToVid, vidFromScope, vidFromString, vidToString } from '../scope/util'
import { VirtualIdentifierMatch, resolveVid } from '../scope/vid'
import { assert } from '../util/todo'
import { nonExhaustiveMatchError, unreachableMatchClauseError } from './error'

Expand All @@ -40,7 +38,7 @@ export type MatchNode = MatchType | MatchVariant | Exhaustive | Unmatched

export type MatchType = {
kind: 'type'
ref: VirtualIdentifierMatch<Variant>
ref: Variant
variants: Map<string, MatchTree>
}

Expand Down
10 changes: 0 additions & 10 deletions src/semantic/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,7 @@ import {
resolveTypeImpl,
typeDefToVirtualType
} from '../scope/trait'
import { idToVid, vidEq, vidFromString, vidToString } from '../scope/util'
import { MethodDef, VariantDef, VirtualIdentifier, VirtualIdentifierMatch, resolveVid, typeKinds } from '../scope/vid'
import {
VidType,
VirtualFnType,
VirtualType,
combine,
extractConcreteSupertype,
isAssignable,
typeToVirtual
} from '../typecheck'
import {
instanceGenericMap,
makeFnGenericMap,
Expand Down
11 changes: 0 additions & 11 deletions src/semantic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ import { Block, BreakStmt, FnDef, ImplDef, ReturnStmt, Statement, TraitDef, VarD
import { Generic, Type } from '../ast/type'
import { TypeDef, Variant } from '../ast/type-def'
import {
BlockScope,
Context,
DefinitionMap,
FnDefScope,
InstanceScope,
Scope,
TypeDefScope,
addError,
addWarning,
defKey,
Expand All @@ -20,12 +15,7 @@ import {
leaveScope,
unwindScope
} from '../scope'
import { InstanceRelation, findSuperRelChains } from '../scope/trait'
import { idToVid, vidEq, vidToString } from '../scope/util'
import { Definition, MethodDef, NameDef, VirtualIdentifierMatch, resolveVid, typeKinds } from '../scope/vid'
import { VirtualFnType, VirtualType, genericToVirtual, isAssignable, typeEq, typeToVirtual } from '../typecheck'
import { instanceGenericMap, makeFnGenericMap, makeGenericMapOverStructure, resolveType } from '../typecheck/generic'
import { holeType, neverType, selfType, unitType, unknownType } from '../typecheck/type'
import { assert, todo, unreachable } from '../util/todo'
import {
argCountMismatchError,
Expand Down Expand Up @@ -57,7 +47,6 @@ import { checkExpr, checkQualifiedMethodCall, checkResolvedClosureExpr } from '.
import { checkPattern } from './match'
import { typeNames } from './type-def'
import { Upcast, UpcastFn, makeUpcast, upcast } from './upcast'
import { VirtualUseExpr, useExprToVids } from './use-expr'

export type Checked = {
checked: boolean
Expand Down
3 changes: 0 additions & 3 deletions src/semantic/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import { Name, Operand } from '../ast/operand'
import { Type } from '../ast/type'
import { Context, addError } from '../scope'
import { getInstanceForType, resolveMethodImpl, resolveTypeImpl } from '../scope/trait'
import { vidFromScope, vidFromString, vidToString } from '../scope/util'
import { MethodDef, VirtualIdentifier, resolveVid, typeKinds } from '../scope/vid'
import { VirtualFnType, VirtualType, combine, genericToVirtual, typeToVirtual } from '../typecheck'
import {
makeFnGenericMap,
makeFnTypeArgGenericMap,
makeGenericMapOverStructure,
resolveType
} from '../typecheck/generic'
import { selfType } from '../typecheck/type'
import { assert } from '../util/todo'
import {
narrowFieldAccessError,
Expand Down
4 changes: 0 additions & 4 deletions src/semantic/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { ConPattern, Pattern } from '../ast/match'
import { Name } from '../ast/operand'
import { Context, addError, defKey } from '../scope'
import { list } from '../scope/std'
import { idToVid, vidEq, vidToString } from '../scope/util'
import { NameDef, resolveVid } from '../scope/vid'
import { VidType, VirtualFnType, VirtualType, isAssignable } from '../typecheck'
import { makeGenericMapOverStructure, resolveType } from '../typecheck/generic'
import { unknownType } from '../typecheck/type'
import { unreachable } from '../util/todo'
import {
nonDestructurableTypeError,
Expand Down
34 changes: 17 additions & 17 deletions src/semantic/op.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { AstNodeKind } from '../ast'
import { vidFromString } from '../scope/util'
import { VirtualIdentifier } from '../scope/vid'
import { Identifier } from '../ast/operand'
import { idFromString } from '../typecheck'

export const operatorImplMap: Map<AstNodeKind, VirtualIdentifier> = new Map([
['add-op', vidFromString('std::num::Num::add')],
['sub-op', vidFromString('std::num::Num::sub')],
['mult-op', vidFromString('std::num::Num::mult')],
['div-op', vidFromString('std::num::Num::div')],
['exp-op', vidFromString('std::num::Num::exp')],
['eq-op', vidFromString('std::eq::Eq::eq')],
['ne-op', vidFromString('std::eq::Eq::ne')],
['ge-op', vidFromString('std::ord::Ord::ge')],
['le-op', vidFromString('std::ord::Ord::le')],
['gt-op', vidFromString('std::ord::Ord::gt')],
['lt-op', vidFromString('std::ord::Ord::lt')],
['and-op', vidFromString('std::bool::Bool::and')],
['or-op', vidFromString('std::bool::Bool::or')],
['mod-op', vidFromString('std::int::Int::mod')]
export const operatorImplMap: Map<AstNodeKind, Identifier> = new Map([
['add-op', idFromString('std::num::Num::add')],
['sub-op', idFromString('std::num::Num::sub')],
['mult-op', idFromString('std::num::Num::mult')],
['div-op', idFromString('std::num::Num::div')],
['exp-op', idFromString('std::num::Num::exp')],
['eq-op', idFromString('std::eq::Eq::eq')],
['ne-op', idFromString('std::eq::Eq::ne')],
['ge-op', idFromString('std::ord::Ord::ge')],
['le-op', idFromString('std::ord::Ord::le')],
['gt-op', idFromString('std::ord::Ord::gt')],
['lt-op', idFromString('std::ord::Ord::lt')],
['and-op', idFromString('std::bool::Bool::and')],
['or-op', idFromString('std::bool::Bool::or')],
['mod-op', idFromString('std::int::Int::mod')]
])
2 changes: 0 additions & 2 deletions src/semantic/semantic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { Package } from '../package'
import { buildModule } from '../package/build'
import { buildPackage } from '../package/io'
import { Context, pathToVid } from '../scope'
import { buildInstanceRelations } from '../scope/trait'
import { vidToString } from '../scope/util'
import { Source } from '../source'
import { checkModule, checkTopLevelDefinition, prepareModule } from './index'

Expand Down

0 comments on commit c07c715

Please sign in to comment.