Skip to content

Commit

Permalink
Add support for execution of other langs
Browse files Browse the repository at this point in the history
  • Loading branch information
dhzdhd committed Apr 15, 2022
1 parent 033f640 commit 0e59de2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
3 changes: 3 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:excode/src/home/services/api.dart';

import 'src/factory.dart';
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
Expand All @@ -9,6 +11,7 @@ import 'src/settings/services/settings_service.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
initialise();
await ApiHandler.initRuntimeVersionData();

final settingsController = SettingsController(SettingsService());
await settingsController.loadSettings();
Expand Down
61 changes: 41 additions & 20 deletions lib/src/home/services/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ enum Languages {
crystal,
dart,
dash,
denoTS,
nodeTS,
denoJS,
nodeJS,
basic,
fSharp,
Expand Down Expand Up @@ -78,37 +76,60 @@ enum Languages {
}

class ApiHandler {
static const _url = 'https://emkc.org/api/v2/piston/execute';
static const _executeUrl = 'https://emkc.org/api/v2/piston/execute';
static const _runtimeUrl = 'https://emkc.org/api/v2/piston/runtimes';
static final Map<String, String> _data = {};

static Future<void> initRuntimeVersionData() async {
final res = await dio.get(_runtimeUrl);
for (var i in res.data) {
_data[i['language']] = i['version'];
}
}

static Map<String, dynamic> getDataFromLang(Languages lang) {
switch (lang) {
case Languages.python:
return {'version': '3.10.0'};
case Languages.coffeeScript:
return {'language': 'coffeescript', 'version': _data['coffeescript']};
case Languages.nodeTS:
return {'language': 'typescript', 'version': _data['typescript']};
case Languages.nodeJS:
return {'language': 'javascript', 'version': _data['javascript']};
case Languages.basic:
return {'language': 'basic.net', 'version': _data['basic.net']};
case Languages.cSharp:
return {'language': 'csharp.net', 'version': _data['csharp.net']};
case Languages.fSharp:
return {'language': 'fsharp.net', 'version': '5.0.201'};
case Languages.rust:
return {'version': '1.50.0'};
return {'language': 'fsharp.net', 'version': _data['fsharp.net']};
case Languages.cpp:
return {'language': 'c++', 'version': _data['c++']};
case Languages.golfScript:
return {'language': 'golfscript', 'version': _data['golfscript']};
case Languages.oCaml:
return {'language': 'ocaml', 'version': _data['ocaml']};
default:
return {'language': 'python', 'version': '3.10.0'};
return {'language': 'python', 'version': _data['python']};
}
}

static Future<Map<String, String>> executeCode(
Languages lang, String content) async {
late final Response res;
final data = {
'language': lang.name,
'version': _data.containsKey(lang.name) ? _data[lang.name] : '',
'files': [
{'content': content}
],
'stdin': '',
'args': ['1', '2', '3'],
'compile_timeout': 10000,
'run_timeout': 3000,
};
if (!_data.containsKey(lang.name)) data.addAll(getDataFromLang(lang));

try {
res = await dio.post(_url, data: {
'language': lang.name,
...getDataFromLang(lang),
'files': [
{'content': content}
],
'stdin': '',
'args': ['1', '2', '3'],
'compile_timeout': 10000,
'run_timeout': 3000,
});
res = await dio.post(_executeUrl, data: data);
} on DioError catch (err) {
print(err.message);
return {'error': err.message};
Expand Down
5 changes: 2 additions & 3 deletions lib/src/home/services/language.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,8 @@ Map<String, TextStyle> getThemeFromEnum(Themes theme) {
String getThemeLangFromEnum(Languages lang) {
switch (lang) {
case Languages.cobol: // ?
case Languages.denoTS:
case Languages.nodeTS:
return 'typescript';
case Languages.denoJS:
case Languages.nodeJS:
case Languages.golfScript:
return 'javascript';
Expand Down Expand Up @@ -227,6 +225,7 @@ String getThemeLangFromEnum(Languages lang) {
case Languages.vlang: // ?
case Languages.zig: // ?
return 'markdown';
default:
return Languages.values[lang.index].name.toLowerCase();
}
return Languages.values[lang.index].name.toLowerCase();
}

0 comments on commit 0e59de2

Please sign in to comment.