Skip to content

Commit 5ad930f

Browse files
authored
Merge pull request #32 from ut-code/cpp-docs
C++のドキュメント追加
2 parents edce53b + bb7aa4b commit 5ad930f

File tree

17 files changed

+3466
-302
lines changed

17 files changed

+3466
-302
lines changed

app/pagesList.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,18 @@ export const pagesList = [
2525
lang: "C++",
2626
description: "C++の基本から高度な機能までを学べるチュートリアル",
2727
pages: [
28+
{ id: 1, title: "C++の世界へようこそ" },
2829
{ id: 2, title: "型システムとメモリ" },
2930
{ id: 3, title: "関数と参照" },
31+
{ id: 4, title: "ポインタと動的メモリ" },
32+
{ id: 5, title: "クラスの基礎" },
33+
{ id: 6, title: "クラスを使いこなす" },
34+
{ id: 7, title: "継承とポリモーフィズム" },
35+
{ id: 8, title: "テンプレート" },
36+
{ id: 9, title: "STL ①:コンテナ" },
37+
{ id: 10, title: "STL ②:アルゴリズムとラムダ式"},
38+
{ id: 11, title: "RAIIとスマートポインタ" },
39+
{ id: 12, title: "プロジェクトの分割とビルド" },
3040
],
3141
},
3242
] as const;

app/terminal/exec.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export type ExecLang = "python" | "cpp";
1818
interface ExecProps {
1919
/*
2020
* Pythonの場合はメインファイル1つのみを指定する。
21-
* C++の場合はソースコード(.cpp)とヘッダー(.h)を全部指定し、ExecFile内で拡張子を元にソースコードと追加コードを分ける
21+
* C++の場合はソースコード(.cpp)を全部指定する
2222
*/
2323
filenames: string[];
2424
language: ExecLang;
@@ -63,17 +63,9 @@ export function ExecFile(props: ExecProps) {
6363
if (!props.filenames || props.filenames.length === 0) {
6464
throw new Error("C++の実行には filenames プロパティが必要です");
6565
}
66-
commandline = wandbox.cppOptions
67-
? `${wandbox.cppOptions.commandline} ${props.filenames.join(" ")} && ./a.out`
68-
: "";
66+
commandline = wandbox.getCommandlineStr("C++", props.filenames);
6967
runtimeInitializing = false;
70-
const namesSource = props.filenames!.filter((name) =>
71-
name.endsWith(".cpp")
72-
);
73-
const namesAdditional = props.filenames!.filter(
74-
(name) => !name.endsWith(".cpp")
75-
);
76-
exec = () => wandbox.runFiles("C++", namesSource, namesAdditional);
68+
exec = () => wandbox.runFiles("C++", props.filenames);
7769
break;
7870
default:
7971
props.language satisfies never;

app/terminal/repl.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { useSectionCode } from "../[docs_id]/section";
1616
import { Terminal } from "@xterm/xterm";
1717

1818
export interface ReplOutput {
19-
type: "stdout" | "stderr" | "error" | "return" | "system"; // 出力の種類
19+
type: "stdout" | "stderr" | "error" | "return" | "trace" | "system"; // 出力の種類
2020
message: string; // 出力メッセージ
2121
}
2222
export interface ReplCommand {
@@ -41,6 +41,9 @@ export function writeOutput(
4141
case "error":
4242
term.write(chalk.red(message));
4343
break;
44+
case "trace":
45+
term.write(chalk.blue.italic(message));
46+
break;
4447
case "system":
4548
term.write(systemMessageColor(message));
4649
break;

app/terminal/wandbox/api.ts

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import { type Fetcher } from "swr";
2+
import { type ReplOutput } from "../repl";
3+
4+
const WANDBOX = "https://wandbox.org";
5+
// https://github.com/melpon/wandbox/blob/ajax/kennel2/API.rst <- 古いけど、説明と例がある
6+
// https://github.com/melpon/wandbox/blob/master/feline/src/types.rs
7+
/* RustのVec<u8>はバイト配列ですが、serialize_with = "serialize_utf8"という指定があるため、
8+
JSONにシリアライズされる際にはUTF-8文字列に変換されると解釈し、TypeScriptの型はstringとします。
9+
by gemini
10+
*/
11+
export interface SwitchOption {
12+
name: string;
13+
"display-flags": string;
14+
"display-name": string;
15+
}
16+
export interface SwitchSingle {
17+
type: "single";
18+
name: string;
19+
"display-name": string;
20+
"display-flags": string;
21+
default: boolean;
22+
}
23+
export interface SwitchSelect {
24+
type: "select";
25+
name: string;
26+
options: SwitchOption[];
27+
default: string;
28+
}
29+
/**
30+
* Rustの 'Switch' enum に対応するディスクリミネேテッドユニオン型です。
31+
* 'type' プロパティの値によって `SwitchSingle` か `SwitchSelect` かを判別できます。
32+
*/
33+
export type Switch = SwitchSingle | SwitchSelect;
34+
export interface CompilerInfo {
35+
name: string;
36+
version: string;
37+
language: string;
38+
"display-name": string;
39+
templates: string[];
40+
"compiler-option-raw": boolean;
41+
"runtime-option-raw": boolean;
42+
"display-compile-command": string;
43+
switches: Switch[];
44+
}
45+
interface Code {
46+
file: string;
47+
code: string;
48+
}
49+
export interface CompileParameter {
50+
compiler: string;
51+
code: string;
52+
codes?: Code[];
53+
options?: string;
54+
stdin?: string;
55+
"compiler-option-raw"?: string;
56+
"runtime-option-raw"?: string;
57+
github_user?: string;
58+
title?: string;
59+
description?: string;
60+
save?: boolean;
61+
created_at?: number;
62+
is_private?: boolean;
63+
"compiler-info"?: CompilerInfo;
64+
}
65+
export interface CompileResult {
66+
status: string;
67+
signal: string;
68+
compiler_output: string;
69+
compiler_error: string;
70+
compiler_message: string;
71+
program_output: string;
72+
program_error: string;
73+
program_message: string;
74+
permlink: string;
75+
url: string;
76+
}
77+
78+
export const compilerInfoFetcher: Fetcher<CompilerInfo[]> = () =>
79+
fetch(new URL("/api/list.json", WANDBOX)).then(
80+
(res) => res.json() as Promise<CompilerInfo[]>
81+
);
82+
83+
interface CompileProps {
84+
compilerName: string;
85+
compilerOptions: string[];
86+
compilerOptionsRaw: string[];
87+
codes: Code[];
88+
}
89+
export interface CompileResultWithOutput extends CompileResult {
90+
compilerOutput: ReplOutput[];
91+
compilerError: ReplOutput[];
92+
programOutput: ReplOutput[];
93+
programError: ReplOutput[];
94+
}
95+
96+
export async function compileAndRun(
97+
options: CompileProps
98+
): Promise<CompileResultWithOutput> {
99+
const result: CompileResult = await fetch(
100+
new URL("/api/compile.json", WANDBOX),
101+
{
102+
method: "post",
103+
headers: {
104+
"Content-Type": "application/json",
105+
},
106+
body: JSON.stringify({
107+
compiler: options.compilerName,
108+
code: "",
109+
codes: options.codes,
110+
options: options.compilerOptions.join(","),
111+
stdin: "",
112+
"compiler-option-raw": options.compilerOptionsRaw.join("\n"),
113+
"runtime-option-raw": "",
114+
save: false,
115+
is_private: true,
116+
} satisfies CompileParameter),
117+
}
118+
).then((res) => res.json());
119+
return {
120+
...result,
121+
compilerOutput: result.compiler_output.trim()
122+
? result.compiler_output
123+
.trim()
124+
.split("\n")
125+
.map((line) => ({ type: "stdout" as const, message: line }))
126+
: [],
127+
compilerError: result.compiler_error.trim()
128+
? result.compiler_error
129+
.trim()
130+
.split("\n")
131+
.map((line) => ({ type: "error" as const, message: line }))
132+
: [],
133+
programOutput: result.program_output.trim()
134+
? result.program_output
135+
.trim()
136+
.split("\n")
137+
.map((line) => ({ type: "stdout" as const, message: line }))
138+
: [],
139+
programError: result.program_error.trim()
140+
? result.program_error
141+
.trim()
142+
.split("\n")
143+
.map((line) => ({ type: "error" as const, message: line }))
144+
: [],
145+
};
146+
}

0 commit comments

Comments
 (0)