about summary refs log blame commit diff stats
path: root/linux/apps/raytracing/1.mu
blob: dfb4ec1391186ee978377b809efe1813c4ad2412 (plain) (tree)
1
2
3
4
5
6
7
8
9




                                                                             

                                      

                     
                       


















                          
# Listing 1 of https://raytracing.github.io/books/RayTracingInOneWeekend.html
# (simplified)
#
# To run (on Linux):
#   $ git clone https://github.com/akkartik/mu
#   $ cd mu/linux
#   $ ./translate apps/raytracing/1.mu
#   $ ./a.elf > 1.ppm

fn main -> _/ebx: int {
  print-string 0, "P3\n256 256\n255\n"
  var j/ecx: int <- copy 0xff
  {
    compare j, 0
    break-if-<
    var i/eax: int <- copy 0
    {
      compare i, 0xff
      break-if->
      print-int32-decimal 0, i
      print-string 0, " "
      print-int32-decimal 0, j
      print-string 0, " 64\n"
      i <- increment
      loop
    }
    j <- decrement
    loop
  }
  return 0
}
n>http.Request) { err := r.ParseForm() if err != nil { log500(w, r, err) return } if r.FormValue("q") != "" || r.FormValue("url") != "" { err := apiEndpointQuery(w, r) if err != nil { log500(w, r, err) return } log200(r) return } // if there's no query, return everything in // registry for a given endpoint var out []string switch r.URL.Path { case "/api/plain/users": out, err = twtxtCache.QueryUser("") case "/api/plain/mentions": out, err = twtxtCache.QueryInStatus("@<") default: out, err = twtxtCache.QueryAllStatuses() } data := parseQueryOut(out) if err != nil { data = []byte("") } etag := fmt.Sprintf("%x", sha256.Sum256(data)) w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) _, err = w.Write(data) if err != nil { log500(w, r, err) return } log200(r) } // handles POST for "/api/plain/users" func apiEndpointPOSTHandler(w http.ResponseWriter, r *http.Request) { apiPostUser(w, r) } // handles "/api/plain/tags" func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) { out, err := twtxtCache.QueryInStatus("#") if err != nil { log500(w, r, err) return } data := parseQueryOut(out) etag := fmt.Sprintf("%x", sha256.Sum256(data)) w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) _, err = w.Write(data) if err != nil { log500(w, r, err) return } log200(r) } // handles "/api/plain/tags/[a-zA-Z0-9]+" func apiTagsHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) tags := vars["tags"] tags = strings.ToLower(tags) out, err := twtxtCache.QueryInStatus("#" + tags) if err != nil { log500(w, r, err) return } tags = strings.Title(tags) out2, err := twtxtCache.QueryInStatus("#" + tags) if err != nil { log500(w, r, err) return } tags = strings.ToUpper(tags) out3, err := twtxtCache.QueryInStatus("#" + tags) if err != nil { log500(w, r, err) return } out = append(out, out2...) out = append(out, out3...) out = uniq(out) data := parseQueryOut(out) etag := fmt.Sprintf("%x", sha256.Sum256(data)) w.Header().Set("ETag", etag) w.Header().Set("Content-Type", txtutf8) _, err = w.Write(data) if err != nil { log500(w, r, err) return } log200(r) } // Serving the stylesheet virtually because // files aren't served directly in getwtxt. func cssHandler(w http.ResponseWriter, r *http.Request) { // Sending the sha256 sum of the modtime in hexadecimal for the ETag header etag := fmt.Sprintf("%x", sha256.Sum256([]byte(staticCache.cssMod.String()))) w.Header().Set("ETag", "\""+etag+"\"") w.Header().Set("Content-Type", cssutf8) pingAssets() n, err := w.Write(staticCache.css) if err != nil || n == 0 { log500(w, r, err) return } log200(r) }