Files
2nd/10_Wiki/Topics/Domain_Programming/AI_and_ML/Macros (매크로).md
T
Antigravity Agent c24165b8bc refactor(topics): 멀티 에이전트용 지식 재편 — _Common(공통 기본기) + Domain_* 구조
에이전트 8종(대화형/프로그래머 C·S/디자이너/설계자/기획자/QA/PD/PM)에게
[공통 기본 능력 + 롤별 Specialty] 2층으로 지식을 주입하기 위한 재분류.
문서 내용·포맷은 무수정, 폴더 이동만 (6,372개 문서 수 보존 확인).

- Topic_Programming → Domain_Programming (내부 구조 보존)
- Topic_Graphic → Domain_Design
- Topic_Business → Domain_Product
- Topic_General → Domain_General
- _Common 신설: Math(구 Topic_Math_Specialty), Reasoning(구 General/From_Thinking & Reasoning),
  Reasoning_Creativity(구 General/From_창의성), Communication(Poetic_Blog_Writing + From_writing)
- 타 도메인의 From_* 폴더는 유지 (출처 표기일 뿐, 이미 도메인에 맞게 분류된 문서)
- 빈 폴더 정리 (memory/procedures)
- 에이전트→폴더 매핑은 workspace의 .astra/agent-knowledge-map.json (9개 에이전트)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 11:05:56 +09:00

4.2 KiB

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-macros-매크로 Macros (매크로) 10_Wiki/Topics verified self
Macros
Metaprogramming
Code Generation
Procedural Macros
none A 0.9 applied
programming
metaprogramming
rust
lisp
c
language-design
2026-05-10 pending
language framework
rust-lisp-c language-features

Macros (매크로)

매 한 줄

"매 컴파일 타임에 코드를 쓴다". 매크로는 텍스트/AST 단위로 코드를 생성하는 메타프로그래밍이며, hygiene/표현력에 따라 C 텍스트 치환 → Rust AST → Lisp의 동형성으로 진화한다.

매 핵심

매 분류

  • Text-level: C/C++ 전처리기 — 토큰 치환, hygiene 없음.
  • AST-level (declarative): Rust macro_rules!, Scheme syntax-rules — 패턴 매칭.
  • AST-level (procedural): Rust proc-macro, OCaml ppx — 임의 컴파일타임 코드.
  • Homoiconic: Lisp/Clojure — 코드 = 데이터, defmacro로 자유 변환.
  • Reflection / templates: C++ template, Zig comptime — 매크로 인접.

매 핵심 속성

  1. Hygiene: 매크로 안의 식별자가 호출처 스코프와 충돌하지 않음.
  2. Phase: 컴파일타임 vs 런타임.
  3. Expressiveness: 단순 치환부터 임의 컴파일러 코드까지.
  4. Tooling: rust-analyzer/clangd가 macro expansion 지원.
  5. Cost: 컴파일 시간 / 디버깅 난이도 trade-off.

💻 패턴

Pattern 1 — Rust macro_rules! (declarative)

macro_rules! vec_of {
    ($($x:expr),*) => {{
        let mut v = Vec::new();
        $( v.push($x); )*
        v
    }};
}
let v = vec_of![1, 2, 3];

Pattern 2 — Rust derive (proc-macro)

#[derive(Debug, Clone, Serialize, Deserialize)]
struct User { id: u64, name: String }
// serde_derive가 컴파일타임에 impl Serialize 생성

Pattern 3 — Rust attribute proc-macro

#[tokio::main]
async fn main() { ... }
// 함수 본체를 async runtime 부트스트랩으로 변환

Pattern 4 — Lisp defmacro

(defmacro unless (cond &body body)
  `(if (not ,cond) (progn ,@body)))
(unless (zerop x) (print "non-zero"))

Pattern 5 — C 매크로 (주의)

#define MAX(a,b) ((a) > (b) ? (a) : (b))   // 괄호 필수
#define SQUARE(x) ((x)*(x))
// MAX(i++, j++) → 부작용 두 번 평가 — 함정

Pattern 6 — Zig comptime (현대 대안)

fn maxOf(comptime T: type, a: T, b: T) T {
    return if (a > b) a else b;
}
// 매크로 없이 타입 매개변수화

Pattern 7 — Rust quote! (proc-macro 작성)

use quote::quote;
let expanded = quote! {
    impl #name { fn id(&self) -> u64 { self.id } }
};
TokenStream::from(expanded)

매 결정 기준

상황 도구
반복 boilerplate macro_rules! 또는 derive
복잡 코드 생성 (DSL, ORM) proc-macro
단순 상수 / 헤더 가드 const / module (매크로 X)
C에서 inline 강제 static inline (매크로보다 안전)
코드 = 데이터 변형 Lisp/Clojure
컴파일타임 일반화 Zig comptime, C++ constexpr/templates

기본값: Rust → 가능하면 함수/제네릭, 그 다음 declarative macro, 마지막 proc-macro.

🔗 Graph

🤖 LLM 활용

언제:

  • macro_rules! 패턴 작성.
  • proc-macro skeleton (syn/quote 보일러).
  • C 매크로 함정 감사.

언제 X:

  • 매크로 디버깅 (실제 expansion 확인 필요).
  • 매우 복잡한 DSL 설계 (테스트 필수).

안티패턴

  • 함수로 충분한데 매크로 사용 (디버깅 지옥).
  • C 매크로에서 인자 괄호 누락.
  • 부작용 있는 인자를 매크로에 넘김 (MAX(i++, j)).
  • proc-macro 컴파일 시간 폭증 무시.
  • Hygiene 가정 (C는 없음, Rust/Scheme은 있음).
  • 매크로로 타입 검사 우회.

🧪 검증 / 중복

  • Verified. Rust 1.83/2024 edition 기준. 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup