-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Continue Updating Claro Docs to Use Auto-Validated Examples
- Loading branch information
1 parent
ba686b4
commit 2fced72
Showing
21 changed files
with
360 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...k_docs/src/metaprogramming/code_reuse/abstract_modules/abstract_modules.tmpl.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
# Abstract Modules | ||
|
||
## Override Flexibility | ||
|
||
# TODO(steving) Make an example of an animal in "WitnessProtection". That doesn't share the name. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletions
9
mdbook_docs/src/metaprogramming/code_reuse/reusing_module_apis/animals_example.claro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
# animals_example.claro | ||
var happyDog = Dog::create("Milo", true); | ||
var unhappyDog = Dog::create("Fido", false); | ||
var cat = Cat::create("Garfield", "This is worse than Monday morning."); | ||
var animals: [oneof<Cat::State, Dog::State>] = [ | ||
Dog::create("Milo", true), | ||
Dog::create("Fido", false), | ||
Cat::create("Garfield", "This is worse than Monday morning.") | ||
]; | ||
|
||
var animals: [oneof<Cat::State, Dog::State>] = [happyDog, unhappyDog, cat]; | ||
for (animal in animals) { | ||
print(AnimalSounds::AnimalSounds::makeNoise(animal)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
mdbook_docs/src/metaprogramming/code_reuse/reusing_module_apis/macro_animals/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# BUILD | ||
load(":animals.bzl", "Animal") | ||
exports_files(["animals.bzl", "BUILD"], visibility = ["//mdbook_docs/src/metaprogramming/code_reuse/reusing_module_apis:__pkg__"]) | ||
exports_files(glob(["*"]), visibility = ["//mdbook_docs/src/metaprogramming/code_reuse:__subpackages__"]) | ||
|
||
Animal(name = "dog", srcs = ["dog.claro"]) | ||
Animal(name = "cat", srcs = ["cat.claro"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 106 additions & 1 deletion
107
mdbook_docs/src/metaprogramming/code_reuse/reusing_source_code/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,113 @@ | ||
load("//:rules.bzl", "claro_module", "claro_binary") | ||
load("//mdbook_docs:docs_with_validated_examples.bzl", "doc_with_validated_examples") | ||
load("//mdbook_docs/src/metaprogramming/code_reuse/reusing_module_apis/macro_animals:animals.bzl", "Animal") | ||
|
||
doc_with_validated_examples( | ||
name = "reusing_source_code", | ||
doc_template = "reusing_source_code.tmpl.md", | ||
examples = [], | ||
examples = [ | ||
{ | ||
"example": "//mdbook_docs/src/metaprogramming/code_reuse/reusing_module_apis:animals_example.claro", | ||
"deps": { | ||
"AnimalSounds": "//stdlib/utils/abstract_modules/example/animal:animal_sounds", | ||
"Cat": "//mdbook_docs/src/metaprogramming/code_reuse/reusing_module_apis/animals:cat", | ||
"Dog": "//mdbook_docs/src/metaprogramming/code_reuse/reusing_module_apis/animals:dog", | ||
} | ||
}, | ||
{ | ||
"example": "//mdbook_docs/src/metaprogramming/code_reuse/reusing_module_apis:macro_BUILD_without_exports", | ||
"executable": False, | ||
"codeblock_css_class": "python", | ||
}, | ||
{ | ||
"example": "cat.claro", | ||
"executable": False, | ||
"codeblock_css_class": "claro", | ||
}, | ||
{ | ||
"example": ":demo_output", | ||
"executable": False, | ||
}, | ||
{ | ||
"example": "get_message_with_name.claro", | ||
"hidden_setup": ["setup.claro", "cat-defer.claro"], | ||
"deps": { | ||
"AnimalSounds": "//stdlib/utils/abstract_modules/example/animal:animal_sounds", | ||
}, | ||
"append_output": False, | ||
}, | ||
{ | ||
"example": "example_shared_src_BUILD", | ||
"executable": False, | ||
"codeblock_css_class": "python", | ||
}, | ||
{ | ||
"example": "cat-defer.claro", | ||
"hidden_setup": ["setup.claro", "get_message_with_name.claro"], | ||
"deps": { | ||
"AnimalSounds": "//stdlib/utils/abstract_modules/example/animal:animal_sounds", | ||
}, | ||
"append_output": False, | ||
}, | ||
{ | ||
"example": "default_animal_sounds_impl.claro", | ||
"hidden_setup": ["setup.claro", "cat-inheritance.claro"], | ||
"deps": { | ||
"AnimalSounds": "//stdlib/utils/abstract_modules/example/animal:animal_sounds", | ||
}, | ||
"append_output": False, | ||
}, | ||
{ | ||
"example": "cat-inheritance.claro", | ||
"hidden_setup": ["setup.claro", "default_animal_sounds_impl.claro"], | ||
"deps": { | ||
"AnimalSounds": "//stdlib/utils/abstract_modules/example/animal:animal_sounds", | ||
}, | ||
"append_output": False, | ||
}, | ||
{ | ||
"example": "example_inheritance_BUILD", | ||
"executable": False, | ||
"codeblock_css_class": "python", | ||
}, | ||
], | ||
) | ||
|
||
Animal(name = "cat", srcs = ["cat-impl.claro"]) | ||
Animal(name = "dog", srcs = ["dog.claro"]) | ||
|
||
claro_binary( | ||
name = "demo", | ||
main_file = "//mdbook_docs/src/metaprogramming/code_reuse/reusing_module_apis:animals_example.claro", | ||
deps = { | ||
"AnimalSounds": "//stdlib/utils/abstract_modules/example/animal:animal_sounds", | ||
"Cat": ":cat", | ||
"Dog": ":dog", | ||
} | ||
) | ||
genrule( | ||
name = "demo_output", | ||
outs = ["demo_output.txt"], | ||
srcs = [":demo_deploy.jar"], | ||
cmd = "$(JAVA) -jar $(SRCS) > $(OUTS)", | ||
tools = ["@bazel_tools//tools/jdk:current_java_runtime"], | ||
toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"], | ||
) | ||
|
||
genrule( | ||
name = "copy", | ||
outs = ["{0}.claro_module_api".format(name) for name in ["animal", "cat_cons", "dog_cons"]], | ||
srcs = [ | ||
"//mdbook_docs/src/metaprogramming/code_reuse/reusing_module_apis/macro_animals:animal.claro_module_api", | ||
"//mdbook_docs/src/metaprogramming/code_reuse/reusing_module_apis/macro_animals:cat_cons.claro_module_api", | ||
"//mdbook_docs/src/metaprogramming/code_reuse/reusing_module_apis/macro_animals:dog_cons.claro_module_api", | ||
], | ||
cmd = "cp $(SRCS) $(RULEDIR)", | ||
) | ||
|
||
genrule( | ||
name = "cat_impl", | ||
outs = ["cat-impl.claro"], | ||
srcs = ["cat.claro"], | ||
cmd = "cat $(SRCS) | sed 's/\\$$//g' > $(OUTS)", | ||
) |
16 changes: 16 additions & 0 deletions
16
mdbook_docs/src/metaprogramming/code_reuse/reusing_source_code/cat-defer.claro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# cat.claro | ||
$$newtype InternalState : struct { favoriteInsult: string } | ||
$$ | ||
implement AnimalSounds::AnimalSounds<State> { | ||
function makeNoise(cat: State) -> string { | ||
var noise = unwrap(unwrap(cat).internal).favoriteInsult; | ||
return getMessageWithName(noise, cat); # Analogous code repeated in dog.claro. | ||
} | ||
} | ||
$$ | ||
$$function create(name: string, favoriteInsult: string) -> State { | ||
$$ return State({ | ||
$$ name = name, | ||
$$ internal = InternalState({favoriteInsult = favoriteInsult}) | ||
$$ }); | ||
$$} |
14 changes: 14 additions & 0 deletions
14
mdbook_docs/src/metaprogramming/code_reuse/reusing_source_code/cat-inheritance.claro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# cat.claro | ||
$$newtype InternalState : struct { favoriteInsult: string } | ||
$$ | ||
function makeNoiseImpl(cat: State) -> string { | ||
# No more code duplication. | ||
return unwrap(unwrap(cat).internal).favoriteInsult; | ||
} | ||
$$ | ||
$$function create(name: string, favoriteInsult: string) -> State { | ||
$$ return State({ | ||
$$ name = name, | ||
$$ internal = InternalState({favoriteInsult = favoriteInsult}) | ||
$$ }); | ||
$$} |
17 changes: 17 additions & 0 deletions
17
mdbook_docs/src/metaprogramming/code_reuse/reusing_source_code/cat.claro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# cat.claro | ||
newtype InternalState : struct { favoriteInsult: string } | ||
|
||
implement AnimalSounds::AnimalSounds<State> { | ||
function makeNoise(cat: State) -> string { | ||
# Cats are mean, they're going to say mean things no matter what. | ||
var noise = unwrap(unwrap(cat).internal).favoriteInsult; | ||
return "{noise} - says {unwrap(cat).name}"; # Analogous code repeated in dog.claro. | ||
} | ||
} | ||
$$ | ||
$$function create(name: string, favoriteInsult: string) -> State { | ||
$$ return State({ | ||
$$ name = name, | ||
$$ internal = InternalState({favoriteInsult = favoriteInsult}) | ||
$$ }); | ||
$$} |
6 changes: 6 additions & 0 deletions
6
..._docs/src/metaprogramming/code_reuse/reusing_source_code/default_animal_sounds_impl.claro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# default_animal_sounds_impl.claro | ||
implement AnimalSounds::AnimalSounds<State> { | ||
function makeNoise(state: State) -> string { | ||
return "{makeNoiseImpl(state)} - says {unwrap(state).name}"; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
mdbook_docs/src/metaprogramming/code_reuse/reusing_source_code/dog.claro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# dog.claro | ||
newtype InternalState : struct { isHappy: boolean } | ||
|
||
implement AnimalSounds::AnimalSounds<State> { | ||
function makeNoise(dog: State) -> string { | ||
var noise: string; | ||
if (unwrap(unwrap(dog).internal).isHappy) { | ||
noise = "Woof!"; | ||
} else { | ||
noise = "Grrrr..."; | ||
} | ||
return "{noise} - says {unwrap(dog).name}"; # Analogous code in cat.claro. | ||
} | ||
} | ||
|
||
function create(name: string, isHappy: boolean) -> State { | ||
return State({ | ||
name = name, | ||
internal = InternalState({isHappy = isHappy}) | ||
}); | ||
} |
6 changes: 6 additions & 0 deletions
6
mdbook_docs/src/metaprogramming/code_reuse/reusing_source_code/example_inheritance_BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# BUILD | ||
load(":animals.bzl", "Animal") | ||
|
||
# An example of **LITERALLY** reusing code. | ||
Animal(name = "dog", srcs = ["dog.claro", "default_animal_sounds_impl.claro"]) | ||
Animal(name = "cat", srcs = ["cat.claro", "default_animal_sounds_impl.claro"]) |
6 changes: 6 additions & 0 deletions
6
mdbook_docs/src/metaprogramming/code_reuse/reusing_source_code/example_shared_src_BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# BUILD | ||
load(":animals.bzl", "Animal") | ||
|
||
# An example of **LITERALLY** reusing code. | ||
Animal(name = "dog", srcs = ["dog.claro", "get_message_with_name.claro"]) | ||
Animal(name = "cat", srcs = ["cat.claro", "get_message_with_name.claro"]) |
5 changes: 5 additions & 0 deletions
5
mdbook_docs/src/metaprogramming/code_reuse/reusing_source_code/get_message_with_name.claro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# get_message_with_name.claro | ||
function getMessageWithName(message: string, state: State) -> string { | ||
var name = unwrap(state).name; # All animal States have a top-level `name` field. | ||
return "{message} - says {name}"; | ||
} |
Oops, something went wrong.