Skip to content

Commit

Permalink
path
Browse files Browse the repository at this point in the history
  • Loading branch information
michele-nuzzi committed Feb 7, 2025
1 parent 96f46a7 commit 3e4282d
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class ArrayLikeDeconstr
readonly rest: Identifier | undefined,
readonly type: PebbleAstType | undefined, // just for the type checker, ususally this is inferred
readonly initExpr: PebbleExpr | undefined,
public flags: number,
readonly range: SourceRange,
) {}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CommonFlags } from "../../../../../common";
import { SourceRange } from "../../../../Source/SourceRange";
import { Identifier } from "../../../common/Identifier";
import { PebbleExpr } from "../../../expr/PebbleExpr";
Expand All @@ -15,6 +16,7 @@ export class NamedDeconstructVarDecl
readonly rest: Identifier | undefined,
readonly type: PebbleAstType | undefined, // can be undefined when use ad function parameter
readonly initExpr: PebbleExpr | undefined, // can be undefined when use ad function parameter
public flags: CommonFlags,
readonly range: SourceRange,
) {}

Expand All @@ -31,6 +33,7 @@ export class NamedDeconstructVarDecl
unnamed.rest,
unnamed.type,
unnamed.initExpr,
unnamed.flags,
range
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CommonFlags } from "../../../../../common";
import { SourceRange } from "../../../../Source/SourceRange";
import { Identifier } from "../../../common/Identifier";
import { PebbleExpr } from "../../../expr/PebbleExpr";
Expand All @@ -12,15 +13,17 @@ export class SimpleVarDecl
readonly name: Identifier,
readonly type: PebbleAstType | undefined,
readonly initExpr: PebbleExpr | undefined,
public flags: CommonFlags,
readonly range: SourceRange,
) {}

static onlyIdentifier( identifier: Identifier )
static onlyIdentifier( identifier: Identifier, flags: CommonFlags ): SimpleVarDecl
{
return new SimpleVarDecl(
identifier,
undefined,
undefined,
flags,
identifier.range
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CommonFlags } from "../../../../../common";
import { SourceRange } from "../../../../Source/SourceRange";
import { Identifier } from "../../../common/Identifier";
import { PebbleExpr } from "../../../expr/PebbleExpr";
Expand All @@ -21,6 +22,7 @@ export class SingleDeconstructVarDecl
readonly rest: Identifier | undefined,
readonly type: PebbleAstType | undefined,
readonly initExpr: PebbleExpr | undefined,
public flags: CommonFlags,
readonly range: SourceRange
) {}
}
6 changes: 5 additions & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ export enum CommonFlags {

Object.freeze(CommonFlags);

export const WIN = globalThis.process && globalThis.process.platform === "win32";
let hasGlobalThis: boolean;
try {
hasGlobalThis = typeof globalThis !== "undefined"
} catch { hasGlobalThis = false; }
export const WIN = hasGlobalThis && globalThis.process && globalThis.process.platform === "win32";
export const EOL = WIN ? "\r\n" : "\n";
export const SEP = WIN ? "\\" : "/";

Expand Down
20 changes: 8 additions & 12 deletions src/compiler/AstCompiler/AstCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import { CompilerOptions } from "../../IR/toUPLC/CompilerOptions";
import { Parser } from "../../parser/Parser";
import { CompilerIoApi, createMemoryCompilerIoApi } from "../io/CompilerIoApi";
import { IPebbleCompiler } from "../IPebbleCompiler";
import { mangleInternalPath } from "../path/mangleInternalPath";
import { Path, removeSingleDotDirsFromPath, resolveAsRootPath } from "../path/path";
import { DependencyGraph } from "./DependencyGrapth";
import { getInternalPath, Path, resolveProjAbsolutePath } from "../path/path";

class ResolveStackNode {
constructor(
Expand Down Expand Up @@ -94,8 +92,8 @@ export class AstCompiler extends DiagnosticEmitter
isEntry: boolean = true
)
{
const internalPath = mangleInternalPath( removeSingleDotDirsFromPath( path ) );
srcText = srcText ?? await this.io.readFile( path, this.rootPath );
const internalPath = getInternalPath( path );
srcText = srcText ?? await this.io.readFile( internalPath, this.rootPath );
if( !srcText )
{
this.error(
Expand Down Expand Up @@ -125,7 +123,7 @@ export class AstCompiler extends DiagnosticEmitter
if(!( src instanceof Source ))
{
src = src.toString();
src = mangleInternalPath( removeSingleDotDirsFromPath( src ) );
src = getInternalPath( src );
src = (await this.sourceFromInternalPath( src ))!;
if( !src ) return this.diagnostics;
}
Expand Down Expand Up @@ -194,7 +192,7 @@ export class AstCompiler extends DiagnosticEmitter
const importStmt = req.dependent.statements.find( stmt => {
if( !isImportStmtLike( stmt ) ) return false;

const asRootPath = resolveAsRootPath(
const asRootPath = resolveProjAbsolutePath(
stmt.fromPath.string,
req.dependent.internalPath
);
Expand All @@ -203,9 +201,7 @@ export class AstCompiler extends DiagnosticEmitter
return asRootPath === prevPath
}) as ImportStmtLike | undefined;

prevPath = mangleInternalPath(
removeSingleDotDirsFromPath( req.dependent.internalPath )
);
prevPath = getInternalPath( req.dependent.internalPath );
if( !importStmt )
{
this.error(
Expand Down Expand Up @@ -271,7 +267,7 @@ export class AstCompiler extends DiagnosticEmitter
{
return stmts
.map( imp => {
const internalPath = resolveAsRootPath(
const internalPath = resolveProjAbsolutePath(
imp.fromPath.string,
requestingPath
);
Expand All @@ -283,7 +279,7 @@ export class AstCompiler extends DiagnosticEmitter
);
return "";
}
return mangleInternalPath( internalPath );
return getInternalPath( internalPath );
})
.filter( path => path !== "" );
}
Expand Down
28 changes: 28 additions & 0 deletions src/compiler/AstCompiler/desugar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { PebbleStmt } from "../../ast/nodes/statements/PebbleStmt";
import { VarStmt } from "../../ast/nodes/statements/VarStmt";

export function desugarVarStatements( stmts: PebbleStmt[] ): void
{
for( let i = 0; i < stmts.length; ++i )
{
const stmt = stmts[ i ];

if( stmt instanceof VarStmt )
{
if( stmt.declarations.length > 1 )
{
const newStmts: PebbleStmt[] = [];

for( const decl of stmt.declarations )
{
newStmts.push( new VarStmt( [ decl ], decl.range ) );
}

stmts.splice( i, 1, ...newStmts );
i--;
continue; // desugar single declarations
}
if( stmt.declarations.length <= 0 ) throw new Error( "Empty VarStmt" );
}
}
}
6 changes: 3 additions & 3 deletions src/compiler/path/__tests__/compiler.path.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { removeSingleDotDirsFromPath, dirname, resolveAsRootPath } from "../path";
import { removeSingleDotDirsFromPath, dirname, resolveProjAbsolutePath } from "../path";

describe("path", () => {

Expand Down Expand Up @@ -61,12 +61,12 @@ describe("path", () => {

});

describe("resolveAsRootPath", () => {
describe("resolveProjAbsolutePath", () => {

function testPath( inp: string, origin: string, out: string | undefined )
{
test(inp + " from " + origin +" -> " + out, () => {
expect( resolveAsRootPath( inp, origin ) ).toEqual( out );
expect( resolveProjAbsolutePath( inp, origin ) ).toEqual( out );
});
}

Expand Down
15 changes: 0 additions & 15 deletions src/compiler/path/mangleInternalPath.ts

This file was deleted.

19 changes: 18 additions & 1 deletion src/compiler/path/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,30 @@ import {
} from "../../utils/text";

import {
extension,
PATH_DELIMITER
} from "../../common";

const separator = CharCode.Slash;

export type Path = string;

export function getInternalPath( path: string ): string
{
return mangleInternalPath( removeSingleDotDirsFromPath( path ) );
}

/** Mangles an external to an internal path. */
export function mangleInternalPath(path: string): string {
if (path.endsWith( extension ))
path = path.substring( 0, path.length - extension.length );
if (path.endsWith("/")) {
path += "index";
} else if (path.endsWith(extension)) {
path = path.substring(0, path.length - extension.length);
}
return path;
}

/**
* Normalizes the specified path, removing interior placeholders.
Expand Down Expand Up @@ -112,7 +129,7 @@ export function removeSingleDotDirsFromPath(path: string): string {
}

/** Resolves the specified path relative to the specified origin. */
export function resolveAsRootPath( toResolve: string, fromPath: string ): string | undefined
export function resolveProjAbsolutePath( toResolve: string, fromPath: string ): string | undefined
{
const fromDirname = dirname( fromPath )
toResolve = removeSingleDotDirsFromPath( toResolve );
Expand Down
Loading

0 comments on commit 3e4282d

Please sign in to comment.