Skip to content

Commit e480833

Browse files
author
draveness
committed
feat: initilize code snippet for metaprogramming
0 parents  commit e480833

20 files changed

+294
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a.out
2+
macro
3+
macro_with_hash

Makefile

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
c:
2+
gcc example.c && ./a.out
3+
4+
c1:
5+
gcc macro.c && ./a.out
6+
7+
c2:
8+
gcc macro_problem.c && ./a.out
9+
10+
c2e:
11+
gcc -E macro_problem.c
12+
13+
h1:
14+
gcc hygiene_macro.c && ./a.out
15+
16+
h1e:
17+
gcc -E hygiene_macro.c
18+
19+
c3:
20+
gcc macro_with_paren.c && ./a.out
21+
22+
e1:
23+
iex macro.exs
24+
25+
r1:
26+
rustc macro.rs && ./macro
27+
28+
r2:
29+
rustc macro_with_hash.rs && ./macro_with_hash
30+

car.rb

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
class CarModel
2+
def engine_info=(info)
3+
@engine_info = info
4+
end
5+
6+
def engine_info
7+
@engine_info
8+
end
9+
10+
def engine_price=(price)
11+
@engine_price = price
12+
end
13+
14+
def engine_price
15+
@engine_price
16+
end
17+
18+
def wheel_info=(info)
19+
@wheel_info = info
20+
end
21+
22+
def wheel_info
23+
@wheel_info
24+
end
25+
26+
def wheel_price=(price)
27+
@wheel_price = price
28+
end
29+
30+
def wheel_price
31+
@wheel_price
32+
end
33+
34+
def airbag_info=(info)
35+
@airbag_info = info
36+
end
37+
38+
def airbag_info
39+
@airbag_info
40+
end
41+
42+
def airbag_price=(price)
43+
@airbag_price = price
44+
end
45+
46+
def airbag_price
47+
@airbag_price
48+
end
49+
50+
def alarm_info=(info)
51+
@alarm_info = info
52+
end
53+
54+
def alarm_info
55+
@alarm_info
56+
end
57+
58+
def alarm_price=(price)
59+
@alarm_price = price
60+
end
61+
62+
def alarm_price
63+
@alarm_price
64+
end
65+
66+
def stereo_info=(info)
67+
@stereo_info = info
68+
end
69+
70+
def stereo_info
71+
@stereo_info
72+
end
73+
74+
def stereo_price=(price)
75+
@stereo_price = price
76+
end
77+
78+
def stereo_price
79+
@stereo_price
80+
end
81+
end

car_with_macro.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class CarModel
2+
FEATURES = ["engine", "wheel", "airbag", "alarm", "stereo"]
3+
4+
FEATURES.each do |feature|
5+
define_method("#{feature}_info=") do |info|
6+
instance_variable_set("@#{feature}_info", info)
7+
end
8+
9+
define_method("#{feature}_info") do
10+
instance_variable_get("@#{feature}_info")
11+
end
12+
13+
define_method "feature_price=" do |price|
14+
instance_variable_set("@#{feature}_price", price)
15+
end
16+
17+
define_method("#{feature}_price") do
18+
instance_variable_get("@#{feature}_price")
19+
end
20+
end
21+
end

echo/echo

1.06 MB
Binary file not shown.

echo/main.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
//go:generate echo "hello world"
4+
func main() {
5+
}

example.c

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int main() {
5+
for (int i = 0; i < 10; i++) {
6+
char *echo = (char*)malloc(6 *sizeof(char));
7+
sprintf(echo, "echo %d", i);
8+
system(echo);
9+
}
10+
11+
return 0;
12+
}

ghost_class.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class MyGhostClass
2+
def method_missing(name, *args)
3+
puts "#{name} was called with arguments: #{args.join(',')}"
4+
end
5+
end
6+
7+
m = MyGhostClass.new
8+
m.awesome_method("one", "two")
9+
m.another_method("three", "four")

hygiene_macro.c

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
3+
#define inc(i) do { int a = 0; ++i; } while(0)
4+
5+
int main() {
6+
int a = 4, b = 8;
7+
inc(a);
8+
inc(b);
9+
printf("%d, %d\n", a, b); // => 4, 9 !!
10+
return 0;
11+
}

macro.c

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdio.h>
2+
3+
#define add(a, b) a + b
4+
5+
int main() {
6+
int a = 1;
7+
int b = 2;
8+
printf("%d + %d = %d\n", a, b, add(a, b));
9+
}

macro.exs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule Unless do
2+
def fun_unless(clause, do: expression) do
3+
if(!clause, do: expression)
4+
end
5+
6+
defmacro macro_unless(clause, do: expression) do
7+
quote do
8+
if(!unquote(clause), do: unquote(expression))
9+
end
10+
end
11+
end

macro.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
macro_rules! yo {
2+
($name:expr) => {
3+
println!("Yo {}!", $name);
4+
};
5+
}
6+
7+
fn main() {
8+
yo!("Draven")
9+
}

macro_problem.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
3+
#define add(a, b) a+b
4+
5+
int main() {
6+
int a = 1;
7+
int b = 2;
8+
int c = 3;
9+
printf("(%d + %d) * %d = %d\n", a, b, c, add(a, b) * c);
10+
}

macro_with_hash.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::collections::HashMap;
2+
3+
macro_rules! map {
4+
($( $key:expr => $value:expr ),*) => {{
5+
let mut hm = HashMap::new();
6+
$(hm.insert($key, $value); )*;
7+
hm
8+
}};
9+
}
10+
11+
fn main() {
12+
let user = map!(
13+
"name" => "Draven",
14+
"gender" => "Male"
15+
);
16+
17+
println!("Userr {:?}", user);
18+
}

macro_with_more_paren.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
3+
#define add(a, b) ((a)+(b))
4+
5+
int main() {
6+
int a = 1;
7+
int b = 2;
8+
int c = 3;
9+
printf("(%d + %d) * %d = %d\n", a, b, c, add(a, b) * c);
10+
}

macro_with_paren.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
3+
#define add(a, b) (a+b)
4+
5+
int main() {
6+
int a = 1;
7+
int b = 2;
8+
int c = 3;
9+
printf("(%d + %d) * %d = %d\n", a, b, c, add(a, b) * c);
10+
}

pill/pill.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package painkiller
2+
3+
//go:generate stringer -type=Pill
4+
type Pill int
5+
6+
const (
7+
Placebo Pill = iota
8+
Aspirin
9+
Ibuprofen
10+
Paracetamol
11+
Acetaminophen = Paracetamol
12+
)

pill/pill_string.go

+26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

with_fun_unless.exs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
IO.puts "with function"
2+
Unless.fun_unless true, do: IO.puts "this should never be printed"

with_macro_unless.exs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
IO.puts "with macro"
2+
Unless.macro_unless true, do: IO.puts "this should never be printed"
3+
4+
# expr = quote do: Unless.macro_unless(true, do: IO.puts "this should never be printed")
5+
# Macro.expand_once(expr, __ENV__)

0 commit comments

Comments
 (0)