Skip to content

Commit

Permalink
Rename UseCompiler to CompilerManagerHook
Browse files Browse the repository at this point in the history
  • Loading branch information
ryyppy committed Jul 27, 2020
1 parent e7ab2bf commit 1ccffea
Showing 1 changed file with 36 additions and 35 deletions.
71 changes: 36 additions & 35 deletions common/UseCompiler.re → common/CompilerManagerHook.re
Expand Up @@ -8,7 +8,8 @@
The interface is defined with a finite state and action dispatcher.
*/

module Api = Bs_platform_api;

open Bs_platform_api;

module LoadScript = {
type err;
Expand Down Expand Up @@ -52,8 +53,8 @@ module CdnMeta = {
module FinalResult = {
/* A final result is the last operation the compiler has done, right now this includes... */
type t =
| Conv(Api.ConversionResult.t)
| Comp(Api.CompilationResult.t)
| Conv(ConversionResult.t)
| Comp(CompilationResult.t)
| Nothing;
};

Expand Down Expand Up @@ -119,18 +120,18 @@ type error =

type selected = {
id: string, // The id used for loading the compiler bundle (ideally should be the same as compilerVersion)
apiVersion: Api.Version.t, // The playground API version in use
apiVersion: Version.t, // The playground API version in use
compilerVersion: string,
ocamlVersion: string,
reasonVersion: string,
libraries: array(string),
instance: Api.Compiler.t,
instance: Compiler.t,
};

type ready = {
versions: array(string),
selected,
targetLang: Api.Lang.t,
targetLang: Lang.t,
errors: array(string), // For major errors like bundle loading
result: FinalResult.t,
};
Expand All @@ -140,19 +141,19 @@ type state =
| SetupFailed(string)
| SwitchingCompiler(ready, string, array(string))
| Ready(ready)
| Compiling(ready, (Api.Lang.t, string));
| Compiling(ready, (Lang.t, string));

type action =
| SwitchToCompiler({
id: string,
libraries: array(string),
})
| SwitchLanguage({
lang: Api.Lang.t,
lang: Lang.t,
code: string,
})
| Format(string)
| CompileCode(Api.Lang.t, string);
| CompileCode(Lang.t, string);

let useCompilerManager = () => {
let (state, setState) = React.useState(_ => Init);
Expand Down Expand Up @@ -181,7 +182,7 @@ let useCompilerManager = () => {
| Ready(ready) =>
let instance = ready.selected.instance;
let availableTargetLangs =
Api.Version.availableLanguages(ready.selected.apiVersion);
Version.availableLanguages(ready.selected.apiVersion);

let currentLang = ready.targetLang;

Expand All @@ -195,15 +196,15 @@ let useCompilerManager = () => {
switch (currentLang, lang) {
| (Reason, Res) =>
instance
->Api.Compiler.convertSyntax(
->Compiler.convertSyntax(
~fromLang=Reason,
~toLang=Res,
~code,
)
->Some
| (Res, Reason) =>
instance
->Api.Compiler.convertSyntax(
->Compiler.convertSyntax(
~fromLang=Res,
~toLang=Reason,
~code,
Expand All @@ -216,13 +217,13 @@ let useCompilerManager = () => {
switch (convResult) {
| Some(result) =>
switch (result) {
| Api.ConversionResult.Fail(_)
| ConversionResult.Fail(_)
| Unknown(_, _)
| UnexpectedError(_) => (
FinalResult.Conv(result),
currentLang,
)
| Api.ConversionResult.Success(code) => (Conv(result), l)
| ConversionResult.Success(code) => (Conv(result), l)
}
| None => (Nothing, l)
};
Expand All @@ -241,16 +242,16 @@ let useCompilerManager = () => {
let instance = ready.selected.instance;
let convResult =
switch (ready.targetLang) {
| Res => instance->Api.Compiler.resFormat(code)->Some
| Reason => instance->Api.Compiler.reasonFormat(code)->Some
| Res => instance->Compiler.resFormat(code)->Some
| Reason => instance->Compiler.reasonFormat(code)->Some
| _ => None
};

let result =
switch (convResult) {
| Some(result) =>
switch (result) {
| Api.ConversionResult.Success(success) =>
| ConversionResult.Success(success) =>
// We will only change the result to a ConversionResult
// in case the reformatting has actually changed code
// otherwise we'd loose previous compilationResults, although
Expand All @@ -260,7 +261,7 @@ let useCompilerManager = () => {
} else {
ready.result;
}
| Api.ConversionResult.Fail(_)
| ConversionResult.Fail(_)
| Unknown(_, _)
| UnexpectedError(_) => FinalResult.Conv(result)
}
Expand Down Expand Up @@ -311,23 +312,23 @@ let useCompilerManager = () => {
->Promise.get(result => {
switch (result) {
| Ok () =>
let instance = Api.Compiler.make();
let apiVersion = Api.apiVersion->Api.Version.fromString;
let instance = Compiler.make();
let apiVersion = apiVersion->Version.fromString;

let selected = {
id: latest,
apiVersion,
compilerVersion: instance->Api.Compiler.version,
ocamlVersion: instance->Api.Compiler.ocamlVersion,
reasonVersion: instance->Api.Compiler.reasonVersion,
compilerVersion: instance->Compiler.version,
ocamlVersion: instance->Compiler.ocamlVersion,
reasonVersion: instance->Compiler.reasonVersion,
libraries,
instance,
};

setState(_ => {
Ready({
selected,
targetLang: Api.Version.defaultTargetLang(apiVersion),
targetLang: Version.defaultTargetLang(apiVersion),
versions,
errors: [||],
result: FinalResult.Nothing,
Expand Down Expand Up @@ -372,23 +373,23 @@ let useCompilerManager = () => {
)
});

let instance = Api.Compiler.make();
let apiVersion = Api.apiVersion->Api.Version.fromString;
let instance = Compiler.make();
let apiVersion = apiVersion->Version.fromString;

let selected = {
id: version,
apiVersion,
compilerVersion: instance->Api.Compiler.version,
ocamlVersion: instance->Api.Compiler.ocamlVersion,
reasonVersion: instance->Api.Compiler.reasonVersion,
compilerVersion: instance->Compiler.version,
ocamlVersion: instance->Compiler.ocamlVersion,
reasonVersion: instance->Compiler.reasonVersion,
libraries,
instance,
};

setState(_ => {
Ready({
selected,
targetLang: Api.Version.defaultTargetLang(apiVersion),
targetLang: Version.defaultTargetLang(apiVersion),
versions: ready.versions,
errors: [||],
result: FinalResult.Nothing,
Expand All @@ -406,14 +407,14 @@ let useCompilerManager = () => {

let compResult =
switch (apiVersion) {
| Api.Version.V1 =>
| Version.V1 =>
switch (lang) {
| Api.Lang.OCaml => instance->Api.Compiler.ocamlCompile(code)
| Api.Lang.Reason => instance->Api.Compiler.reasonCompile(code)
| Api.Lang.Res => instance->Api.Compiler.resCompile(code)
| Lang.OCaml => instance->Compiler.ocamlCompile(code)
| Lang.Reason => instance->Compiler.reasonCompile(code)
| Lang.Res => instance->Compiler.resCompile(code)
}
| UnknownVersion(apiVersion) =>
Api.CompilationResult.UnexpectedError(
CompilationResult.UnexpectedError(
{j|Can't handle result of compiler API version "$apiVersion"|j},
)
};
Expand Down

0 comments on commit 1ccffea

Please sign in to comment.