diff --git a/src/java/com/claro/claro_build_rules.bzl b/src/java/com/claro/claro_build_rules.bzl index 09a09e77..cc16435e 100644 --- a/src/java/com/claro/claro_build_rules.bzl +++ b/src/java/com/claro/claro_build_rules.bzl @@ -37,40 +37,59 @@ CLARO_BUILTIN_JAVA_DEPS = [ "//src/java/com/claro/stdlib/userinput", ] -def _claro_binary_RULE_impl(ctx): # TODO(steving) Rename to _claro_binary_impl() once finished w/ dev. +def _claro_binary_impl(ctx): # TODO(steving) Rename to _claro_binary_impl() once finished w/ dev. # The user told me which .claro file is the main file, however, they may have also included it in the srcs list, so # filter it from the srcs. This way the main file is always guaranteed to be the first file in the list. - srcs = [ctx.files.main_file[0]] + [f for f in ctx.files.srcs if f != ctx.file.main_file] - print("Srcs Paths:", [ctx.genfiles_dir.path + '/' + f.path for f in srcs]) + srcs = [ctx.file.main_file] + [f for f in ctx.files.srcs if f != ctx.file.main_file] # By deriving the project package from the workspace name, this rule's able to ensure that generated Java sources # end up using unique Java packages so that it doesn't conflict with any downstream deps. project_package = ctx.workspace_name.replace('-', '.').replace('_', '.') - codegend_java_source = ctx.actions.declare_file(ctx.label.name + ".java") - print("Declaring Action to run Claro compiler to generate:", codegend_java_source) # Declare an Action to execute the Claro compiler binary over the given srcs. # Constructing the args using ctx.actions.args() is Bazel's approach to performance optimization akin to Java's # use of StringBuilder rather than immediate String concatenations. args = ctx.actions.args() args.add("--java_source") - args.add("--silent=" + ("false" if ctx.attr.debug else "true")) - args.add("--package=" + project_package) - args.add_joined("--srcs", srcs, join_with=',') - print("Args:", args) + classname = ctx.file.main_file.basename[:len(ctx.file.main_file.basename) - len(".claro")] + args.add("--classname", classname) + if not ctx.attr.debug: + args.add("--silent") + args.add("--package", project_package) + for stdlib_src in ctx.files._stdlib_srcs: + args.add("--src", stdlib_src) + for src in srcs: + args.add("--src", src) + args.add("--output_file_path", ctx.outputs.compiled_java) + ctx.actions.run( - inputs = srcs, - outputs = [codegend_java_source], + inputs = ctx.files._stdlib_srcs + srcs, + outputs = [ctx.outputs.compiled_java], arguments = [args], - progress_message = "Compiling Claro Program: " + ctx.label.name, + progress_message = "Compiling Claro Program: " + ctx.outputs.compiled_java.short_path, executable = ctx.executable._claro_compiler, ) -# executable = ctx.actions.declare_file(ctx.label.name + "_deploy.jar") -# print("Claro executable: ", executable) -claro_binary_RULE = rule( # TODO(steving) Rename to claro_binary() once finished w/ dev. - implementation = _claro_binary_RULE_impl, +def claro_binary(name, **kwargs): + main_file = kwargs['main_file'] + main_file_name = main_file[:len(main_file) - len(".claro")] + _claro_binary( + name = "{0}_compile".format(name), + compiled_java = "{0}.java".format(main_file_name), + **kwargs + ) + + native.java_binary( + name = name, + main_class = "claro.lang." + main_file_name, + srcs = [":{0}_compile".format(name)], + deps = CLARO_BUILTIN_JAVA_DEPS, + ) + + +_claro_binary = rule( # TODO(steving) Rename to claro_binary() once finished w/ dev. + implementation = _claro_binary_impl, attrs = { "main_file": attr.label( mandatory = True, @@ -79,21 +98,23 @@ claro_binary_RULE = rule( # TODO(steving) Rename to claro_binary() once finished "srcs": attr.label_list(allow_files = [".claro"]), "debug": attr.bool(default = False), "_claro_compiler": attr.label( - default = Label("//src/java/com/claro:claro_compiler_binary_deploy.jar"), - allow_single_file = True, + default = Label("//src/java/com/claro:claro_compiler_binary"), + allow_files = True, executable = True, cfg = "host", ), + "_stdlib_srcs": attr.label_list( + default = [Label(stdlib_file) for stdlib_file in CLARO_STDLIB_FILES], + allow_files = [".claro_internal"], + ), + "compiled_java": attr.output( + doc = "The .java source file codegen'd by the Claro compiler. This is an intermediate output produced by " + + "this rule, and manually inspecting it should not be necessary for most users.", + mandatory = True, + ), }, ) -def claro_binary(name, srcs, java_name): - native.java_binary( - name = name, - main_class = DEFAULT_PACKAGE_PREFIX + "." + java_name, - srcs = srcs, - deps = CLARO_BUILTIN_JAVA_DEPS - ) # This macro produces a target that will allow you to build a claro_builtin_java_deps_deploy.jar that can be used by the # CLI to compile Claro programs from source w/o using Bazel. This is intended for use in lightweight scripting scenarios diff --git a/src/java/com/claro/claro_programs/BUILD b/src/java/com/claro/claro_programs/BUILD index b76a02ad..dea483cd 100644 --- a/src/java/com/claro/claro_programs/BUILD +++ b/src/java/com/claro/claro_programs/BUILD @@ -1,450 +1,186 @@ package(default_visibility = ["//visibility:public"]) -# TODO(steving) Eventually cleanup and move those build targets in here as well. -filegroup( - name = "claro_programs", - srcs = ["first.claro", "second.claro"], -) - load("//src/java/com/claro:claro_build_rules.bzl", "claro_library", "claro_binary", - "claro_binary_RULE", # TODO(steving) Start using the actual rule impl rather than the Macro once dev complete. "gen_claro_builtin_java_deps_jar") # Generate target for the builtin Java deps that'll be needed by the CLI to build w/o Bazel. gen_claro_builtin_java_deps_jar() -claro_binary( - name = "conditions_compiled_claro_image", - srcs = [":conditions_compiled_claro"], - java_name = "Conditions", -) - -claro_library( - name = "conditions_compiled_claro", - # TODO(steving) Fix Riju configuration so that we don't always need to have this file named Conditions. - src = "Conditions.claro", -) - claro_binary( name = "atoms", - srcs = [":atoms_lib"], - java_name = "atoms", -) - -claro_library( - name = "atoms_lib", - src = "atoms.claro", + main_file = "atoms.claro", ) claro_binary( name = "collection_comprehensions", - srcs = [":collection_comprehensions_lib"], - java_name = "collection_comprehensions", -) - -claro_library( - name = "collection_comprehensions_lib", - src = "collection_comprehensions.claro", + main_file = "collection_comprehensions.claro", ) claro_binary( - name = "contracts_compiled_claro_image", - srcs = [":contracts_compiled_claro"], - java_name = "contracts", -) - -claro_library( - name = "contracts_compiled_claro", - src = "contracts.claro", + name = "contracts", + main_file = "contracts.claro", ) claro_binary( name = "contracts_implied_types", - srcs = [":contracts_implied_types_lib"], - java_name = "contracts_implied_types", -) - -claro_library( - name = "contracts_implied_types_lib", - src = "contracts_implied_types.claro", + main_file = "contracts_implied_types.claro", ) claro_binary( name = "copy", - srcs = [":copy_lib"], - java_name = "copy", -) - -claro_library( - name = "copy_lib", - src = "copy.claro", + main_file = "copy.claro", ) claro_binary( - name = "dynamic_dispatch_compiled_claro_image", - srcs = [":dynamic_dispatch_compiled_claro"], - java_name = "dynamic_dispatch", -) - -claro_library( - name = "dynamic_dispatch_compiled_claro", - src = "dynamic_dispatch.claro", + name = "dynamic_dispatch", + main_file = "dynamic_dispatch.claro", ) claro_binary( name = "error_handling_and_propagation", - srcs = [":error_handling_and_propagation_lib"], - java_name = "error_handling_and_propagation", -) - -claro_library( - name = "error_handling_and_propagation_lib", - src = "error_handling_and_propagation.claro", + main_file = "error_handling_and_propagation.claro", ) claro_binary( - name = "generics_compiled_claro_image", - srcs = [":generics_compiled_claro"], - java_name = "generics", -) - -claro_library( - name = "generics_compiled_claro", - src = "generics.claro", + name = "generics", + main_file = "generics.claro", ) claro_binary( - name = "generic_return_inference_experimentation_compiled_claro_image", - srcs = [":generic_return_inference_experimentation_compiled_claro"], - java_name = "generic_return_inference_experimentation", -) - -claro_library( - name = "generic_return_inference_experimentation_compiled_claro", - src = "generic_return_inference_experimentation.claro", + name = "generic_return_inference_experimentation", + main_file = "generic_return_inference_experimentation.claro", ) claro_binary( - name = "exec_compiled_claro_image", - srcs = [":exec_compiled_claro"], - java_name = "exec", -) - -claro_library( - name = "exec_compiled_claro", - src = "exec.claro", + name = "exec", + main_file = "exec.claro", ) claro_binary( name = "for_loops", - srcs = [":for_loops_lib"], - java_name = "for_loops", + main_file = "for_loops.claro", ) -claro_library( - name = "for_loops_lib", - src = "for_loops.claro", -) claro_binary( - name = "format_strings_compiled_claro_image", - srcs = [":format_strings_compiled_claro"], - java_name = "format_strings", -) - -claro_library( - name = "format_strings_compiled_claro", - src = "format_strings.claro", + name = "format_strings", + main_file = "format_strings.claro", ) claro_binary( - name = "graphs_compiled_claro_image", - srcs = [":graphs_compiled_claro"], - java_name = "graphs", -) - -claro_library( - name = "graphs_compiled_claro", - src = "graphs.claro", + name = "graphs", + main_file = "graphs.claro", ) claro_binary( name = "http_requests", - srcs = [":http_requests_lib"], - java_name = "http_requests", -) - -claro_library( - name = "http_requests_lib", - src = "http_requests.claro", + main_file = "http_requests.claro", ) claro_binary( name = "http_server", - srcs = [":http_server_lib"], - java_name = "http_server", -) - -claro_library( - name = "http_server_lib", - src = "http_server.claro", + main_file = "http_server.claro", ) claro_binary( - name = "immutability_vs_mutability", - srcs = [":immutability_vs_mutability_lib"], - java_name = "immutability_vs_mutability", -) - -claro_library( - name = "immutability_vs_mutability_lib", - src = "immutability_vs_mutability.claro", + name = "immutability", + main_file = "immutability_vs_mutability.claro", ) claro_binary( - name = "json_parsing", - srcs = [":json_parsing_lib"], - java_name = "json_parsing", -) - -claro_library( - name = "json_parsing_lib", - src = "json_parsing.claro", + name = "json", + main_file = "json_parsing.claro", ) claro_binary( - name = "keyword_generics_compiled_claro_image", - srcs = [":keyword_generics_compiled_claro"], - java_name = "keyword_generics", -) - -claro_library( - name = "keyword_generics_compiled_claro", - src = "keyword_generics.claro", + name = "keyword", + main_file = "keyword_generics.claro", ) claro_binary( - name = "lambdas_compiled_claro_image", - srcs = [":lambdas_compiled_claro"], - java_name = "lambdas", -) - -claro_library( - name = "lambdas_compiled_claro", - src = "lambdas.claro", + name = "lambdas", + main_file = "lambdas.claro", ) claro_binary( - name = "list_compiled_claro_image", - srcs = [":list_compiled_claro"], - java_name = "list", -) - -claro_library( - name = "list_compiled_claro", - src = "list.claro", + name = "list", + main_file = "list.claro", ) claro_binary( - name = "maps_compiled_claro_image", - srcs = [":maps_compiled_claro"], - java_name = "maps", -) - -claro_library( - name = "maps_compiled_claro", - src = "maps.claro", + name = "maps", + main_file = "maps.claro", ) claro_binary( name = "match", - srcs = [":match_lib"], - java_name = "match", -) - -claro_library( - name = "match_lib", - src = "match.claro", + main_file = "match.claro", ) claro_binary( - name = "modules_compiled_claro_image", - srcs = [":modules_compiled_claro"], - java_name = "modules", -) - -claro_library( - name = "modules_compiled_claro", - src = "modules.claro", + name = "modules", + main_file = "modules.claro", ) claro_binary( - name = "multiple_files", - srcs = [":multiple_files_lib"], - java_name = "multiple_files_A", -) - -claro_library( - name = "multiple_files_lib", - src = [ - "multiple_files_A.claro", - "multiple_files_B.claro" - ], - java_name = "multiple_files_A", + name = "oneof", + main_file = "oneof.claro", ) claro_binary( - name = "oneof_compiled_claro_image", - srcs = [":oneof_compiled_claro"], - java_name = "oneof", -) - -claro_library( - name = "oneof_compiled_claro", - src = "oneof.claro", -) - - -claro_binary( - name = "piping_compiled_claro_image", - srcs = [":piping_compiled_claro"], - java_name = "piping", -) - -claro_library( - name = "piping_compiled_claro", - src = "piping.claro", + name = "piping", + main_file = "piping.claro", ) claro_binary( - name = "procedures_compiled_claro_image", - srcs = [":procedures_compiled_claro"], - java_name = "procedures", -) - -claro_library( - name = "procedures_compiled_claro", - src = "procedures.claro", + name = "procedures", + main_file = "procedures.claro", ) claro_binary( - name = "recursion_compiled_claro_image", - srcs = [":recursion_compiled_claro"], - java_name = "recursion", -) - -claro_library( - name = "recursion_compiled_claro", - src = "recursion.claro", + name = "recursion", + main_file = "recursion.claro", ) # TODO(steving) TESTING!!! DO NOT SUBMIT! THIS IS NOT ACTUALLY WORKING. GOT REVEALED ONCE APPEND WAS MADE INTO A PROPER -# TODO(steving) TESTING!!! FUNCTION INSTEAD OF A COMPILER BUILTIN. +# TODO(steving) TESTING!!! FUNCTION INSTEAD OF A COMPILER BUILTIN. NEED TO DEPRECATE SUPPORT FOR RECURSIVE ALIASES! claro_binary( - name = "recursive_aliases_compiled_claro_image", - srcs = [":recursive_aliases_compiled_claro"], - java_name = "recursive_aliases", -) - -claro_library( - name = "recursive_aliases_compiled_claro", - src = "recursive_aliases.claro", + name = "recursive", + main_file = "recursive_aliases.claro", ) claro_binary( name = "repeat", - srcs = [":repeat_lib"], - java_name = "repeat", -) - -claro_library( - name = "repeat_lib", - src = "repeat.claro", + main_file = "repeat.claro", ) claro_binary( - name = "sets_compiled_claro_image", - srcs = [":sets_compiled_claro"], - java_name = "sets", -) - -claro_library( - name = "sets_compiled_claro", - src = "sets.claro", + name = "sets", + main_file = "sets.claro", ) claro_binary( - name = "structs_compiled_claro_image", - srcs = [":structs_compiled_claro"], - java_name = "structs", -) - -claro_library( - name = "structs_compiled_claro", - src = "structs.claro", + name = "structs", + main_file = "structs.claro", ) claro_binary( - name = "tuples_compiled_claro_image", - srcs = [":tuples_compiled_claro"], - java_name = "tuples", -) - -claro_library( - name = "tuples_compiled_claro", - src = "tuples.claro", + name = "tuples", + main_file = "tuples.claro", ) claro_binary( - name = "types_compiled_claro_image", - srcs = [":types_compiled_claro"], - java_name = "types", -) - -claro_library( - name = "types_compiled_claro", - src = "types.claro", + name = "types", + main_file = "types.claro", ) claro_binary( name = "user_defined_types", - srcs = [":user_defined_types_lib"], - java_name = "user_defined_types", -) - -claro_library( - name = "user_defined_types_lib", - src = "user_defined_types.claro", + main_file = "user_defined_types.claro", ) claro_binary( - name = "while_test_compiled_claro_image", - srcs = [":while_test_compiled_claro"], - java_name = "while_test", + name = "while", + main_file = "while_test.claro", ) - -claro_library( - name = "while_test_compiled_claro", - src = "while_test.claro", -) - -# Test Claro's fledgling First-Class Module system. -claro_library( - name = "module_test", - module_api_file = "TestModule.claro_module_api", - src = [ - "TestModuleImpl.claro", - "TestModuleImpl2.claro", - ], - debug = True, -) - -# Test Claro's in-dev `claro_binary_RULE` which is going to replace the `claro_binary` macro. -claro_binary_RULE( - name = "test", - main_file = "TestModuleImpl.claro", - srcs = [ - "TestModuleImpl2.claro", - ], -) \ No newline at end of file diff --git a/src/java/com/claro/claro_programs/first.claro b/src/java/com/claro/claro_programs/first.claro deleted file mode 100644 index ad3cefe3..00000000 --- a/src/java/com/claro/claro_programs/first.claro +++ /dev/null @@ -1,60 +0,0 @@ -# First calculator source file! - -var VARX = (log_3.1(8) / log_3.1(2)) ** 2 + 1.1 * 2; -print(VARX); - -var Y = VARX + 2; -print(Y); -if (Y > 10) { - Y = Y * 100; - print("Y > 10"); - if (Y < 10 ** 5) { - print("Y < 10 ** 2"); - print("cool, multi-statement, nested conditions work."); - } -} -if (Y <= 10) { - print("Y <= 10"); -} -print(2 - Y); -print(2 - Y > 1 - Y == true); - -# Let's test out scoping in conditions. -var scopeTest1 = 1; -print(scopeTest1); -if (scopeTest1 == 1) { - var scopeTest2 = 2; - print(scopeTest1); - print(scopeTest2); - if (scopeTest2 == 2) { - var scopeTest3 = 3; - print(scopeTest1); - print(scopeTest2); - print(scopeTest3); - } - print(scopeTest1); - print(scopeTest2); -} -print(scopeTest1); -# Uncomment the line below to see an error about referencing before assignment. -# print(scopeTest2); - -# Let's test out condition chains. -if (false) { - print("False shouldn't be printing..."); -} else if (true) { - if (false) { - print("This shouldn't be printing..."); - } else if (false) { - print("This shouldn't be printing..."); - } else { - print("This should print!"); - } -} else if (true) { - print("Ok this code is real weird, but anyways, this shouldn't print"); -} else { - print("How are we even getting here...at some point this should throw for unreachable code probably..."); -} - -# Uncomment the line below to see an error for unused symbols. -# var UNUSED = 9; diff --git a/src/java/com/claro/claro_programs/modules/BUILD b/src/java/com/claro/claro_programs/modules/BUILD new file mode 100644 index 00000000..a3b600b2 --- /dev/null +++ b/src/java/com/claro/claro_programs/modules/BUILD @@ -0,0 +1,12 @@ +load("//src/java/com/claro:claro_build_rules.bzl", "claro_library") + +# Test Claro's fledgling First-Class Module system. +claro_library( + name = "module_test", + module_api_file = "TestModule.claro_module_api", + src = [ + "TestModuleImpl.claro", + "TestModuleImpl2.claro", + ], + debug = True, +) diff --git a/src/java/com/claro/claro_programs/TestModule.claro_module_api b/src/java/com/claro/claro_programs/modules/TestModule.claro_module_api similarity index 100% rename from src/java/com/claro/claro_programs/TestModule.claro_module_api rename to src/java/com/claro/claro_programs/modules/TestModule.claro_module_api diff --git a/src/java/com/claro/claro_programs/TestModuleImpl.claro b/src/java/com/claro/claro_programs/modules/TestModuleImpl.claro similarity index 74% rename from src/java/com/claro/claro_programs/TestModuleImpl.claro rename to src/java/com/claro/claro_programs/modules/TestModuleImpl.claro index ba74386a..ba00969c 100644 --- a/src/java/com/claro/claro_programs/TestModuleImpl.claro +++ b/src/java/com/claro/claro_programs/modules/TestModuleImpl.claro @@ -6,3 +6,5 @@ function intIdentity(i: int) -> int { function add(lhs: int, rhs: int) -> int { return lhs + rhs; } + +print("Just validate that this works."); \ No newline at end of file diff --git a/src/java/com/claro/claro_programs/TestModuleImpl2.claro b/src/java/com/claro/claro_programs/modules/TestModuleImpl2.claro similarity index 100% rename from src/java/com/claro/claro_programs/TestModuleImpl2.claro rename to src/java/com/claro/claro_programs/modules/TestModuleImpl2.claro diff --git a/src/java/com/claro/claro_programs/second.claro b/src/java/com/claro/claro_programs/second.claro deleted file mode 100644 index 6461f510..00000000 --- a/src/java/com/claro/claro_programs/second.claro +++ /dev/null @@ -1,28 +0,0 @@ -# This language has booleans and numbers at the moment. - -# Print out and show a boolean. -print( - log_2(9 ** 2 + 51 - 4) == 7.0 -); - -# Show the ability to convert from boolean to a number (0 or 1). -print( - numeric_bool(5 == 3 + 2) + 4 -); - -print("testing Z"); -var Z: string = input("Sup bruh, gimme a number:"); -print(Z); -print(Z == "10"); -print(Z != "10"); - -print(""); - -var z2: float = 11.0; -print("testing z2"); -print(z2); -print(z2 > 10); -print(z2 < 10); -print(z2 <= 10); -print(z2 >= 10); -print((z2 == 10.0 + 1) == true); \ No newline at end of file diff --git a/src/java/com/claro/claro_programs/test_multi_file/BUILD b/src/java/com/claro/claro_programs/test_multi_file/BUILD new file mode 100644 index 00000000..7f2255af --- /dev/null +++ b/src/java/com/claro/claro_programs/test_multi_file/BUILD @@ -0,0 +1,9 @@ +load("//src/java/com/claro:claro_build_rules.bzl", "claro_binary") + +claro_binary( + name = "multiple_files", + main_file = "multiple_files_A.claro", + srcs = [ + "multiple_files_B.claro", + ], +) \ No newline at end of file diff --git a/src/java/com/claro/claro_programs/multiple_files_A.claro b/src/java/com/claro/claro_programs/test_multi_file/multiple_files_A.claro similarity index 100% rename from src/java/com/claro/claro_programs/multiple_files_A.claro rename to src/java/com/claro/claro_programs/test_multi_file/multiple_files_A.claro diff --git a/src/java/com/claro/claro_programs/multiple_files_B.claro b/src/java/com/claro/claro_programs/test_multi_file/multiple_files_B.claro similarity index 100% rename from src/java/com/claro/claro_programs/multiple_files_B.claro rename to src/java/com/claro/claro_programs/test_multi_file/multiple_files_B.claro diff --git a/src/java/com/claro/compiler_backends/interpreted/Interpreter.java b/src/java/com/claro/compiler_backends/interpreted/Interpreter.java index 64bb1a39..615bf4a7 100644 --- a/src/java/com/claro/compiler_backends/interpreted/Interpreter.java +++ b/src/java/com/claro/compiler_backends/interpreted/Interpreter.java @@ -25,8 +25,8 @@ public void run() throws Exception { if (!this.SILENT) { System.out.println("Enter your expression:"); } - // TODO(steving) DO NOT SUBMIT until you fix this to figure out a way to take in these files within Bazel scope. - String inputFile = "com/claro/claro_programs/second.claro"; + // TODO(steving) THERE'S NO SUCH FILE. THIS INTERPRETER BACKEND IS COMPLETELY ABANDONED FOR NOW... + String inputFile = "to_interpret.claro"; Scanner scan = null; try { scan = new Scanner(new File(inputFile)); diff --git a/src/java/com/claro/compiler_backends/java_source/JavaSourceCompilerBackend.java b/src/java/com/claro/compiler_backends/java_source/JavaSourceCompilerBackend.java index b4ffe05d..a8195b0b 100644 --- a/src/java/com/claro/compiler_backends/java_source/JavaSourceCompilerBackend.java +++ b/src/java/com/claro/compiler_backends/java_source/JavaSourceCompilerBackend.java @@ -21,6 +21,8 @@ import com.google.devtools.common.options.OptionsParser; import com.google.protobuf.ByteString; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; @@ -36,6 +38,7 @@ public class JavaSourceCompilerBackend implements CompilerBackend { private final Optional PACKAGE_STRING; private final ImmutableList SRCS; private final Optional OPTIONAL_UNIQUE_MODULE_NAME; + private final Optional OPTIONAL_OUTPUT_FILE_PATH; // TODO(steving) Migrate this file to use an actual cli library. // TODO(steving) Consider Apache Commons Cli 1.4 https://commons.apache.org/proper/commons-cli/download_cli.cgi @@ -59,6 +62,8 @@ public JavaSourceCompilerBackend(String... args) { .collect(ImmutableList.toImmutableList()); this.OPTIONAL_UNIQUE_MODULE_NAME = Optional.ofNullable(options.unique_module_name.isEmpty() ? null : options.unique_module_name); + this.OPTIONAL_OUTPUT_FILE_PATH = + Optional.ofNullable(options.output_file_path.isEmpty() ? null : options.output_file_path); } private static JavaSourceCompilerBackendCLIOptions parseCLIOptions(String... args) { @@ -186,9 +191,26 @@ private Node.GeneratedJavaSource checkTypesAndGenJavaSourceForSrcFiles( nonMainSrcFiles ); } else { - // Here, we were simply asked to codegen an executable Claro program. Output the codegen'd Java source to - // stdout directly where it will be piped by Claro's Bazel rules into the appropriate .java file. - System.out.println(generateTargetOutputRes); + if (this.OPTIONAL_OUTPUT_FILE_PATH.isPresent()) { + // Here we've been asked to write the output to a particular file. + File outputFile = null; + try { + outputFile = new File(this.OPTIONAL_OUTPUT_FILE_PATH.get()); + outputFile.createNewFile(); + } catch (IOException e) { + System.err.println("An error occurred while trying to open/create the specified output file: " + + this.OPTIONAL_OUTPUT_FILE_PATH.get()); + e.printStackTrace(); + System.exit(1); + } + try (FileWriter outputFileWriter = new FileWriter(outputFile)) { + outputFileWriter.write(generateTargetOutputRes.toString()); + } + } else { + // Here, we were simply asked to codegen an executable Claro program. Output the codegen'd Java source to + // stdout directly where it will be piped by Claro's Bazel rules into the appropriate .java file. + System.out.println(generateTargetOutputRes); + } } System.exit(0); } else { diff --git a/src/java/com/claro/compiler_backends/java_source/JavaSourceCompilerBackendCLIOptions.java b/src/java/com/claro/compiler_backends/java_source/JavaSourceCompilerBackendCLIOptions.java index 09765005..9a7344b0 100644 --- a/src/java/com/claro/compiler_backends/java_source/JavaSourceCompilerBackendCLIOptions.java +++ b/src/java/com/claro/compiler_backends/java_source/JavaSourceCompilerBackendCLIOptions.java @@ -10,7 +10,7 @@ public class JavaSourceCompilerBackendCLIOptions extends OptionsBase { name = "silent", abbrev = 's', help = "Compiler will omit debug output.", - defaultValue = "true" + defaultValue = "false" ) public boolean silent; @@ -44,4 +44,11 @@ public class JavaSourceCompilerBackendCLIOptions extends OptionsBase { defaultValue = "" ) public String unique_module_name; + + @Option( + name = "output_file_path", + help = "The path to the output file to put the generated Java.", + defaultValue = "" + ) + public String output_file_path; } diff --git a/src/java/com/claro/intermediate_representation/types/impls/BUILD b/src/java/com/claro/intermediate_representation/types/impls/BUILD index 418eada5..b50beac6 100644 --- a/src/java/com/claro/intermediate_representation/types/impls/BUILD +++ b/src/java/com/claro/intermediate_representation/types/impls/BUILD @@ -1,6 +1,6 @@ package( default_visibility = [ - "//src/java/com/claro/claro_programs:__pkg__", + "//src/java/com/claro/claro_programs:__subpackages__", "//src/java/com/claro/intermediate_representation/types/impls:__subpackages__", "//src/java/com/claro/runtime_utilities:__pkg__", ] diff --git a/src/java/com/claro/intermediate_representation/types/impls/builtins_impls/BUILD b/src/java/com/claro/intermediate_representation/types/impls/builtins_impls/BUILD index 4fc372bb..b8b82d9c 100644 --- a/src/java/com/claro/intermediate_representation/types/impls/builtins_impls/BUILD +++ b/src/java/com/claro/intermediate_representation/types/impls/builtins_impls/BUILD @@ -1,6 +1,6 @@ package( default_visibility = [ - "//src/java/com/claro/claro_programs:__pkg__", + "//src/java/com/claro/claro_programs:__subpackages__", "//src/java/com/claro/compiler_backends/interpreted:__pkg__", "//src/java/com/claro/intermediate_representation:__subpackages__", "//src/java/com/claro/runtime_utilities:__pkg__",