Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@
**/syntax.tab.h
**/lex.yy.c
**/lexer
/gui/.next
/gui/node_modules
gui/package-lock.json
gui/temp.structura
gui/ir.txt
20 changes: 20 additions & 0 deletions gui/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"dependencies": {
"next": "^15.1.3",
"react": "^19.0.0",
"react-dom": "^19.0.0"
},
"name": "gui",
"version": "1.0.0",
"main": "index.js",
"devDependencies": {},
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}
46 changes: 46 additions & 0 deletions gui/pages/api/compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { exec } from 'child_process';
import fs from 'fs';
import path from 'path';

export default function handler(req, res) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Only POST requests allowed' });
}

const { code } = req.body;

// 写入 Structura 文件
const tempFilePath = path.join(process.cwd(), 'temp.structura');
fs.writeFileSync(tempFilePath, code);

try {
// 运行 structura_parser
exec(`./structura_parser < ${tempFilePath} > ir.txt`, (error, stdout, stderr) => {
if (error) {
return res.status(500).json({ error: stderr || error.message });
}

const irOutput = fs.readFileSync('ir.txt', 'utf8');

// 运行 llc
exec(`llc -march=mips -mcpu=mips32 ir.txt -o ir.s`, (llcError, llcStdout, llcStderr) => {
if (llcError) {
return res.status(500).json({ error: llcStderr || llcError.message });
}

// 读取生成的 MIPS 汇编代码
const mipsOutput = fs.readFileSync('ir.s', 'utf8');

// 清理临时文件
fs.unlinkSync(tempFilePath);
fs.unlinkSync('ir.txt');
fs.unlinkSync('ir.s');

// 返回结果
res.status(200).json({ output: mipsOutput, ir: irOutput });
});
});
} catch (error) {
res.status(500).json({ error: error.message });
}
}
69 changes: 69 additions & 0 deletions gui/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { useState } from 'react';

export default function Home() {
const [code, setCode] = useState('');
const [output, setOutput] = useState('');
const [error, setError] = useState('');
const [loading, setLoading] = useState(false);

const compileCode = async () => {
setLoading(true);
setError('');
setOutput('');

try {
const response = await fetch('/api/compile', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code }),
});

const result = await response.json();
if (response.ok) {
setOutput(result.output);
} else {
setError(result.error || 'Compilation failed.');
}
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
};

return (
<div style={{ padding: '2rem', fontFamily: 'Arial, sans-serif' }}>
<h1>Structura Compiler</h1>
<textarea
rows="10"
cols="80"
value={code}
onChange={(e) => setCode(e.target.value)}
placeholder="Write your Structura code here..."
style={{ width: '100%', marginBottom: '1rem' }}
/>
<button onClick={compileCode} disabled={loading}>
{loading ? 'Compiling...' : 'Compile to MIPS'}
</button>
<div style={{ marginTop: '1rem' }}>
{error && <p style={{ color: 'red' }}>Error: {error}</p>}
{output && (
<div>
<pre
style={{
background: '#f4f4f4',
padding: '1rem',
border: '1px solid #ccc',
}}
>
{output}
</pre>
<pre style={{ background: '#f4f4f4', padding: '1rem', border: '1px solid #ccc' }}>
{ir}
</pre>
</div>
)}
</div>
</div>
);
}
8 changes: 8 additions & 0 deletions gui/vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"version": 2,
"builds": [
{ "src": "pages/api/*.js", "use": "@vercel/node" },
{ "src": "pages/*.js", "use": "@vercel/next" }
]
}

Loading