'use client' import React, { useState, useEffect } from 'react' import { Play, Square, Download, Upload, Settings, Clock, MemoryStick, Cpu } from 'lucide-react' interface ExecutionResult { success: boolean execution_id: string output: string error: string execution_time: number memory_used: number exit_code: number language: string timestamp: string } interface Language { id: string name: string extension: string timeout: number memory_limit: string } export default function RealCompilerInterface() { const [code, setCode] = useState('') const [input, setInput] = useState('') const [output, setOutput] = useState('') const [selectedLanguage, setSelectedLanguage] = useState('python') const [languages, setLanguages] = useState([]) const [isExecuting, setIsExecuting] = useState(false) const [executionResult, setExecutionResult] = useState(null) const [executionHistory, setExecutionHistory] = useState([]) const languageTemplates: { [key: string]: string } = { python: `# Python Code print("Hello World!") name = input("Enter your name: ") print(f"Hello, {name}!")`, java: `public class Main { public static void main(String[] args) { System.out.println("Hello World!"); // Your code here } }`, cpp: `#include #include using namespace std; int main() { cout << "Hello World!" << endl; string name; cout << "Enter your name: "; getline(cin, name); cout << "Hello, " << name << "!" << endl; return 0; }`, c: `#include int main() { printf("Hello World!\\n"); char name[100]; printf("Enter your name: "); fgets(name, sizeof(name), stdin); printf("Hello, %s", name); return 0; }`, javascript: `// JavaScript Code console.log("Hello World!"); const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('Enter your name: ', (name) => { console.log(\`Hello, \${name}!\`); rl.close(); });`, go: `package main import ( "fmt" "bufio" "os" ) func main() { fmt.Println("Hello World!") reader := bufio.NewReader(os.Stdin) fmt.Print("Enter your name: ") name, _ := reader.ReadString('\\n') fmt.Printf("Hello, %s", name) }`, rust: `use std::io; fn main() { println!("Hello World!"); println!("Enter your name: "); let mut name = String::new(); io::stdin().read_line(&mut name).expect("Failed to read line"); println!("Hello, {}!", name.trim()); }` } useEffect(() => { fetchSupportedLanguages() }, []) useEffect(() => { if (selectedLanguage && languageTemplates[selectedLanguage] && !code) { setCode(languageTemplates[selectedLanguage]) } }, [selectedLanguage]) const fetchSupportedLanguages = async () => { try { const response = await fetch('http://127.0.0.1:5000/api/compiler/languages') const data = await response.json() if (data.success) { setLanguages(data.languages) } } catch (error) { console.error('Failed to fetch languages:', error) } } const executeCode = async () => { if (!code.trim()) { alert('Please write some code first!') return } setIsExecuting(true) setOutput('') setExecutionResult(null) try { const response = await fetch('http://127.0.0.1:5000/api/compiler/execute', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ code, language: selectedLanguage, input: input }) }) const result = await response.json() if (result.success) { setOutput(result.output || result.error || 'No output') setExecutionResult(result) // Add to history setExecutionHistory(prev => [result, ...prev.slice(0, 9)]) // Keep last 10 } else { setOutput(`Error: ${result.error}`) } } catch (error) { setOutput(`Execution failed: ${(error as Error).message}`) } finally { setIsExecuting(false) } } const testCompiler = async () => { try { const response = await fetch('http://127.0.0.1:5000/api/compiler/test', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ language: selectedLanguage }) }) const result = await response.json() if (result.success) { setOutput(result.output) alert('Compiler test successful!') } else { setOutput(`Test failed: ${result.error}`) } } catch (error) { setOutput(`Test failed: ${(error as Error).message}`) } } const downloadCode = () => { const language = languages.find(l => l.id === selectedLanguage) const extension = language?.extension || '.txt' const blob = new Blob([code], { type: 'text/plain' }) const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `code${extension}` document.body.appendChild(a) a.click() document.body.removeChild(a) URL.revokeObjectURL(url) } const loadCodeFile = (event: React.ChangeEvent) => { const file = event.target.files?.[0] if (file) { const reader = new FileReader() reader.onload = (e) => { const content = e.target?.result as string setCode(content) } reader.readAsText(file) } } const clearAll = () => { setCode('') setInput('') setOutput('') setExecutionResult(null) } return (
{/* Header */}

OpenLearnX Real Compiler

Execute code in multiple programming languages with real output

{languages.length} languages supported
{/* Code Editor */}
{/* Language Selector & Controls */}

Code Editor