Skip to content

Commit 6ae478e

Browse files
Merge pull request csg-tokyo#85 from maejima-fumika/refactor/std-module
Refactor the builtin module.
2 parents 8bebca7 + 81ac5af commit 6ae478e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+449
-1512
lines changed

bs-utils.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"prepublishOnly": "npm run build"
1010
},
1111
"bin": {
12-
"blue": "./dist/index.js"
12+
"bscript": "./dist/index.js"
1313
},
1414
"files": [
1515
"dist"

cli/src/commands/board/setup.ts

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import chalk from "chalk";
1111

1212

1313
const RUNTIME_ZIP_URL = `https://github.com/csg-tokyo/bluescript/releases/download/v${VM_VERSION}/release-microcontroller-v${VM_VERSION}.zip`;
14-
const GLOBAL_PACKAGES_ZIP_URL = `https://github.com/csg-tokyo/bluescript/releases/download/v${VM_VERSION}/release-modules-v${VM_VERSION}.zip`;
1514
const RUNTIME_DIR = path.join(GLOBAL_BLUESCRIPT_PATH, 'microcontroller');
16-
const GLOBAL_PACKAGES_DIR = path.join(GLOBAL_BLUESCRIPT_PATH, 'modules');
1715

1816
const ESP_IDF_VERSION = 'v5.4';
1917
const ESP_IDF_GIT_REPO = 'https://github.com/espressif/esp-idf.git';
@@ -32,58 +30,40 @@ abstract class SetupHandler {
3230
getSetupPlan(): string[] {
3331
const plan: string[] = [];
3432
plan.push(`Download BlueScript runtime from ${RUNTIME_ZIP_URL}`);
35-
plan.push(`Download global packages from ${GLOBAL_PACKAGES_ZIP_URL}`);
3633
plan.push(...this.getBoardSetupPlan());
3734
return plan;
3835
}
3936

4037
async setup(): Promise<void> {
38+
this.ensureBlueScriptDir();
4139
await this.downloadBlueScriptRuntime();
42-
await this.downloadGlobalPackages();
4340
await this.setupBoard();
4441
this.globalConfigHandler.save();
4542
}
4643

47-
private needToDownloadBlueScriptRuntime() {
48-
return !this.globalConfigHandler.isRuntimeSetup();
44+
private ensureBlueScriptDir() {
45+
if (!fs.exists(GLOBAL_BLUESCRIPT_PATH)) {
46+
fs.makeDir(GLOBAL_BLUESCRIPT_PATH);
47+
}
4948
}
5049

51-
private needToDownloadGlobalPackages() {
52-
return !this.globalConfigHandler.isGlobalPackagesSetup();
50+
private needToDownloadBlueScriptRuntime() {
51+
return !this.globalConfigHandler.isRuntimeSetup();
5352
}
5453

5554
@LogStep(`Downloading BlueScript runtime...`)
5655
private async downloadBlueScriptRuntime() {
5756
if (!this.needToDownloadBlueScriptRuntime()) {
5857
throw new SkipStep('already downloaded.', undefined);
5958
}
60-
6159
if (fs.exists(RUNTIME_DIR)) {
6260
fs.removeDir(RUNTIME_DIR);
6361
}
64-
if (!fs.exists(GLOBAL_BLUESCRIPT_PATH)) {
65-
fs.makeDir(GLOBAL_BLUESCRIPT_PATH);
66-
}
62+
6763
await fs.downloadAndUnzip(RUNTIME_ZIP_URL, GLOBAL_BLUESCRIPT_PATH);
6864
this.globalConfigHandler.setRuntimeDir(RUNTIME_DIR);
6965
}
7066

71-
@LogStep(`Downloading global packages...`)
72-
private async downloadGlobalPackages() {
73-
if (!this.needToDownloadGlobalPackages()) {
74-
throw new SkipStep('already downloaded.', undefined);
75-
}
76-
77-
if (fs.exists(GLOBAL_PACKAGES_DIR)) {
78-
fs.removeDir(GLOBAL_PACKAGES_DIR);
79-
}
80-
if (!fs.exists(GLOBAL_BLUESCRIPT_PATH)) {
81-
fs.makeDir(GLOBAL_BLUESCRIPT_PATH);
82-
}
83-
await fs.downloadAndUnzip(GLOBAL_PACKAGES_ZIP_URL, GLOBAL_BLUESCRIPT_PATH);
84-
this.globalConfigHandler.setGlobalPackagesDir(GLOBAL_PACKAGES_DIR);
85-
}
86-
8767
abstract needSetup(): boolean;
8868

8969
abstract getBoardSetupPlan(): string[];
@@ -210,7 +190,7 @@ export class ESP32SetupHandler extends SetupHandler {
210190
}
211191

212192
fs.makeDir(ESP_ROOT_DIR);
213-
await exec(`git clone -b ${ESP_IDF_VERSION} --recursive ${ESP_IDF_GIT_REPO}`, { cwd: ESP_ROOT_DIR });
193+
await exec(`git clone --depth 1 -b ${ESP_IDF_VERSION} --recursive ${ESP_IDF_GIT_REPO}`, { cwd: ESP_ROOT_DIR });
214194
}
215195

216196
@LogStep('Running ESP-IDF install script...')

cli/src/commands/repl.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
ProjectConfigHandler,
1111
} from "../config/project-config";
1212
import { BleConnection, DeviceService } from "../services/ble";
13-
import { Compiler, ExecutableBinary, MemoryLayout, PackageConfig } from "@bluescript/lang";
13+
import { Compiler, CompilerConfig, ErrorLog, ExecutableBinary, MemoryLayout, PackageConfig } from "@bluescript/lang";
1414
import * as path from 'path';
1515
import * as readline from 'readline';
1616
import chalk from "chalk";
@@ -94,7 +94,12 @@ abstract class ReplHandler {
9494
await this.execute(bin);
9595
this.rl.prompt();
9696
} catch (error) {
97-
reject(error);
97+
if (error instanceof ErrorLog) {
98+
replLogger.error("** compile error: " + error.toString());
99+
this.rl.prompt();
100+
} else {
101+
reject(error);
102+
}
98103
}
99104
});
100105
this.rl.on('close', () => {
@@ -165,22 +170,14 @@ class ESP32ReplHandler extends ReplHandler {
165170
return {bin, time};
166171
}
167172

168-
private getCompilerConfig() {
173+
private getCompilerConfig(): CompilerConfig {
169174
const runtimeDir = this.globalConfigHandler.getConfig().runtimeDir;
170175
if (!runtimeDir) {
171176
throw new Error('An unexpected error occurred: cannot find runtime directory path.');
172177
}
173-
const globalPackagesDir = this.globalConfigHandler.getConfig().globalPackagesDir;
174-
if (!globalPackagesDir) {
175-
throw new Error('An unexpected error occurred: cannot find directory path for global packages.');
176-
}
177-
const stdPackageDir = path.join(globalPackagesDir, 'std');
178178
return {
179-
dirs: {
180-
runtime: runtimeDir,
181-
compilerToolchain: this.boardConfig.xtensaGccDir,
182-
std: stdPackageDir
183-
}
179+
runtimeDir,
180+
compilerToolchainDir: this.boardConfig.xtensaGccDir,
184181
}
185182
}
186183

cli/src/commands/run.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from "../config/project-config";
1212
import { cwd } from "../core/shell";
1313
import { BleConnection, DeviceService } from "../services/ble";
14-
import { Compiler, ExecutableBinary, MemoryLayout, PackageConfig } from "@bluescript/lang";
14+
import { Compiler, CompilerConfig, ExecutableBinary, MemoryLayout, PackageConfig } from "@bluescript/lang";
1515
import * as path from 'path';
1616
import * as readline from 'readline';
1717

@@ -143,24 +143,15 @@ class ESP32RunHandler extends RunHandler {
143143
return {bin, time};
144144
}
145145

146-
private getCompilerConfig() {
146+
private getCompilerConfig(): CompilerConfig {
147147
const runtimeDir = this.projectConfigHandler.getConfig().runtimeDir
148148
?? this.globalConfigHandler.getConfig().runtimeDir;
149149
if (!runtimeDir) {
150150
throw new Error('An unexpected error occurred: cannot find runtime directory path.');
151151
}
152-
const globalPackagesDir = this.projectConfigHandler.getConfig().globalPackagesDir
153-
?? this.globalConfigHandler.getConfig().globalPackagesDir;
154-
if (!globalPackagesDir) {
155-
throw new Error('An unexpected error occurred: cannot find directory path for global packages.');
156-
}
157-
const stdPackageDir = path.join(globalPackagesDir, 'std');
158152
return {
159-
dirs: {
160-
runtime: runtimeDir,
161-
compilerToolchain: this.boardConfig.xtensaGccDir,
162-
std: stdPackageDir
163-
}
153+
runtimeDir,
154+
compilerToolchainDir: this.boardConfig.xtensaGccDir,
164155
}
165156
}
166157

cli/src/config/global-config.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const boardConfigSchema = z.object({
2727
const globalConfigSchema = z.object({
2828
version: z.string().default(VM_VERSION),
2929
runtimeDir: z.string().optional(),
30-
globalPackagesDir: z.string().optional(),
3130
boards: boardConfigSchema.default({}),
3231
});
3332

@@ -103,14 +102,6 @@ export class GlobalConfigHandler {
103102
this.update({runtimeDir: dir});
104103
}
105104

106-
isGlobalPackagesSetup() {
107-
return this.config.globalPackagesDir !== undefined;
108-
}
109-
110-
setGlobalPackagesDir(dir: string) {
111-
this.update({globalPackagesDir: dir});
112-
}
113-
114105
getConfig(): Readonly<GlobalConfig> {
115106
return this.config;
116107
}

cli/tests/commands/board/setup.test.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ describe('board setup command', () => {
3838
// --- Arrange ---
3939
mockGlobalConfigHandler.isBoardSetup.mockReturnValue(false);
4040
mockGlobalConfigHandler.isRuntimeSetup.mockReturnValue(false);
41-
mockGlobalConfigHandler.isGlobalPackagesSetup.mockReturnValue(false);
4241
mockedFs.exists.mockReturnValue(false);
4342
mockedExec.mockImplementation(async (command: string) => {
4443
if (command.startsWith('which')) {
@@ -63,10 +62,9 @@ describe('board setup command', () => {
6362
// 1. Ask user for confirmation
6463
expect(mockedInquirer.prompt).toHaveBeenCalledTimes(1);
6564

66-
// 2. Dwonload runtime and packages
67-
expect(mockedFs.downloadAndUnzip).toHaveBeenCalledTimes(2);
65+
// 2. Dwonload runtime
66+
expect(mockedFs.downloadAndUnzip).toHaveBeenCalledTimes(1);
6867
expect(mockGlobalConfigHandler.setRuntimeDir).toHaveBeenCalled();
69-
expect(mockGlobalConfigHandler.setGlobalPackagesDir).toHaveBeenCalled();
7068

7169
// 3. Install required packages via Homebrew
7270
expect(mockedExec).toHaveBeenCalledWith('brew install cmake ninja dfu-util ccache');
@@ -86,11 +84,10 @@ describe('board setup command', () => {
8684
expect(mockedLogger.error).not.toHaveBeenCalled();
8785
});
8886

89-
it('should skip downloading runtime and packages if they exist', async () => {
87+
it('should skip downloading runtime if it exist', async () => {
9088
// --- Arrange ---
9189
mockGlobalConfigHandler.isBoardSetup.mockReturnValue(false);
9290
mockGlobalConfigHandler.isRuntimeSetup.mockReturnValue(true);
93-
mockGlobalConfigHandler.isGlobalPackagesSetup.mockReturnValue(true);
9491

9592
// --- Act ---
9693
await handleSetupCommand('esp32');
@@ -106,7 +103,6 @@ describe('board setup command', () => {
106103
// --- Arrange ---
107104
mockGlobalConfigHandler.isBoardSetup.mockReturnValue(false);
108105
mockGlobalConfigHandler.isRuntimeSetup.mockReturnValue(false);
109-
mockGlobalConfigHandler.isGlobalPackagesSetup.mockReturnValue(false);
110106
mockedExec.mockImplementation(async (command: string) => {
111107
if (command.startsWith('which')) {
112108
if (command.includes('brew') || command.includes('git')) {
@@ -134,7 +130,6 @@ describe('board setup command', () => {
134130
// --- Arrange ---
135131
mockGlobalConfigHandler.isBoardSetup.mockReturnValue(false);
136132
mockGlobalConfigHandler.isRuntimeSetup.mockReturnValue(false);
137-
mockGlobalConfigHandler.isGlobalPackagesSetup.mockReturnValue(false);
138133
mockedExec.mockImplementation(async (command: string) => {
139134
if (command.startsWith('which')) {
140135
if (command.includes('brew') || command.includes('git')) {

cli/tests/commands/mock-helpers.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ export function setupMocks() {
2626
getConfig: jest.fn(),
2727
isBoardSetup: jest.fn(),
2828
isRuntimeSetup: jest.fn(),
29-
isGlobalPackagesSetup: jest.fn(),
3029
setRuntimeDir: jest.fn(),
31-
setGlobalPackagesDir: jest.fn(),
3230
update: jest.fn(),
3331
updateBoardConfig: jest.fn(),
3432
getBoardConfig: jest.fn(),

lang/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ and install it.
2020
npm install
2121
```
2222

23-
Move to the `./server` directory.
23+
Move to the `./lang` directory.
2424

2525
```bash
26-
cd server
26+
cd lang
2727
```
2828

2929
## Run the REPL
@@ -60,7 +60,7 @@ This reads and runs `foo.bs` and `bar.bs` in this order when the REPL starts run
6060

6161
Note that the REPL separately compiles every code fragment by users.
6262
It performs the reading, compiling, running, and printing loop.
63-
All the temporary files are stored in `./server/temp-files`.
63+
All the temporary files are stored in `./lang/temp-files`.
6464

6565
## Compile a BlueScript program
6666

lang/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"dev": "ts-node src/index.ts",
2929
"exec": "./node_modules/.bin/tsc && node ./dist/index.js",
3030
"shell": "npx tsc && node ./dist/transpiler/shell.js",
31-
"compile": "npx tsc && node ./dist/transpiler/compiler.js",
31+
"transpile-module": "npx tsc && node ./dist/tools/transpile-module.js",
3232
"test": "jest"
3333
}
3434
}

0 commit comments

Comments
 (0)