Warning, /frameworks/syntax-highlighting/autotests/input/highlight.cr is written in an unsupported language. File is not indexed.

0001 # This file is a testcase for the highlighting of Crystal code
0002 # It's not executable code, but a collection of code snippets
0003 #
0004 
0005 require "lib_z"
0006 require "./digest"
0007 
0008 module Digest::Adler32
0009   def self.initial : UInt32
0010     LibZ.adler32(0, nil, 0).to_u32
0011   end
0012 
0013   def self.checksum(data) : UInt32
0014     update(data, initial)
0015   end
0016 
0017   def self.update(data, adler32 : UInt32) : UInt32
0018     slice = data.to_slice
0019     LibZ.adler32(adler32, slice, slice.size).to_u32
0020   end
0021 
0022   def self.combine(adler1 : UInt32, adler2 : UInt32, len) : UInt32
0023     LibZ.adler32_combine(adler1, adler2, len).to_u32
0024   end
0025 end
0026 
0027 struct BigRational
0028   Number.expand_div [Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128], BigRational
0029   Number.expand_div [Float32, Float64], BigRational
0030 end
0031 
0032 module Crystal::Repl::Closure
0033   VAR_NAME = ".closure_var"
0034   ARG_NAME = ".closure_arg"
0035 end
0036 
0037 class FunctionType
0038   getter arg_types : Array(ArgType)
0039   getter return_type : ArgType
0040 
0041   @@cvar = 3
0042   @ivar = 7
0043 
0044   def initialize(@arg_types, @return_type)
0045   end
0046 
0047   def //(other : Int::Unsigned) : BigInt
0048     check_division_by_zero other
0049     unsafe_floored_div(other)
0050   end
0051 
0052   def //(other : Int) : BigInt
0053     check_division_by_zero other
0054 
0055     if other < 0
0056       (-self).unsafe_floored_div(-other)
0057       @cvar += 1
0058     else
0059       unsafe_floored_div(other)
0060      @ivar += 10
0061     end
0062   end
0063 end
0064 
0065 require "llvm/enums/atomic"
0066 struct Atomic(T)
0067   # Creates an Atomic with the given initial value.
0068   def compare_and_set(cmp : T, new : T) : {T, Bool}
0069     {% if T.union? && T.union_types.all? { |t| t == Nil || t < Reference } %}
0070       address, success = Ops.cmpxchg(pointerof(@value).as(LibC::SizeT*), LibC::SizeT.new(cmp.as(T).object_id), LibC::SizeT.new(new.as(T).object_id), :sequentially_consistent, :sequentially_consistent)
0071       {address == 0 ? nil : Pointer(T).new(address).as(T), success}
0072     {% elsif T < Reference %}
0073       # Use addresses again (but this can't return nil)
0074       address, success = Ops.cmpxchg(pointerof(@value).as(LibC::SizeT*), LibC::SizeT.new(cmp.as(T).object_id), LibC::SizeT.new(new.as(T).object_id), :sequentially_consistent, :sequentially_consistent)
0075       {Pointer(T).new(address).as(T), success}
0076     {% else %}
0077       Ops.cmpxchg(pointerof(@value), cmp, new, :sequentially_consistent, :sequentially_consistent)
0078     {% end %}
0079   end
0080 
0081   def swap(value : T)
0082     {% if T.union? && T.union_types.all? { |t| t == Nil || t < Reference } || T < Reference %}
0083       address = Ops.atomicrmw(:xchg, pointerof(@value).as(LibC::SizeT*), LibC::SizeT.new(value.as(T).object_id), :sequentially_consistent, false)
0084       Pointer(T).new(address).as(T)
0085     {% else %}
0086       Ops.atomicrmw(:xchg, pointerof(@value), value, :sequentially_consistent, false)
0087     {% end %}
0088   end
0089 end
0090 
0091 class Deque(T)
0092   include Indexable::Mutable(T)
0093 
0094   @start = 0
0095   protected setter size
0096   private getter buffer
0097 
0098   def initialize(size : Int, value : T)
0099     if size < 0
0100       raise ArgumentError.new("Negative deque size: #{size}")
0101     end
0102     @size = size.to_i
0103     @capacity = size.to_i
0104 
0105     unless @capacity == 0
0106       @buffer = Pointer(T).malloc(@capacity, value)
0107     end
0108   end
0109 
0110   # Returns a new `Deque` that has this deque's elements cloned.
0111   # That is, it returns a deep copy of this deque.
0112   #
0113   # Use `#dup` if you want a shallow copy.
0114   def clone
0115     {% if T == ::Bool || T == ::Char || T == ::String || T == ::Symbol || T < ::Number::Primitive %}
0116       Deque(T).new(size) { |i| self[i].clone.as(T) }
0117     {% else %}
0118       exec_recursive_clone do |hash|
0119         clone = Deque(T).new(size)
0120         each do |element|
0121           clone << element.clone
0122         end
0123         clone
0124       end
0125     {% end %}
0126   end
0127 
0128   def delete_at(index : Int) : T
0129     unless 0 <= index < @size
0130       raise IndexError.new
0131     end
0132     return shift if index == 0
0133 
0134     if index > @size // 2
0135       # Move following items to the left, starting with the first one
0136       # [56-01234] -> [6x-01235]
0137       dst = rindex
0138       finish = (@start + @size - 1) % @capacity
0139       loop do
0140         src = dst + 1
0141         src -= @capacity if src >= @capacity
0142         @buffer[dst] = @buffer[src]
0143         break if src == finish
0144         dst = src
0145       end
0146       (@buffer + finish).clear
0147     end
0148 
0149   end
0150 
0151   def each(& : T ->) : Nil
0152     halfs do |r|
0153       r.each do |i|
0154         yield @buffer[i]
0155       end
0156     end
0157   end
0158 
0159   def pop : T
0160     pop { raise IndexError.new }
0161   end
0162 
0163 
0164   macro [](*args)
0165     array = uninitialized Array(Int32)
0166     {% for arg, i in args %}
0167       array.to_unsafe[{{i}}] = {{arg}}
0168     {% end %}
0169     array
0170   end
0171 
0172     def message : String
0173         case self
0174         when SUCCESS        then "No error occurred. System call completed successfully."
0175         when TXTBSY         then Errno::ETXTBSY
0176         when NOTCAPABLE     then Errno::ENOTCAPABLE
0177         else                     Errno::EINVAL
0178         end
0179     end
0180 
0181     enum Signal
0182         KILL = 0
0183         BILL = 101
0184     end
0185 
0186 end
0187 
0188 
0189   # :nodoc:
0190 module Ops
0191     # Defines methods that directly map to LLVM instructions related to atomic operations.
0192 
0193     @[Primitive(:cmpxchg)]
0194     def self.cmpxchg(ptr : T*, cmp : T, new : T, success_ordering : LLVM::AtomicOrdering, failure_ordering : LLVM::AtomicOrdering) : {T, Bool} forall T
0195     end
0196 
0197     @[Primitive(:atomicrmw)]
0198     def self.atomicrmw(op : LLVM::AtomicRMWBinOp, ptr : T*, val : T, ordering : LLVM::AtomicOrdering, singlethread : Bool) : T forall T
0199     end
0200 end
0201 
0202 
0203 @[Link("z")]
0204 lib LibZ
0205   alias Char = LibC::Char
0206   alias SizeT = LibC::SizeT
0207 
0208   fun zlibVersion : Char*
0209   fun crc32_combine(crc1 : ULong, crc2 : ULong, len : Long) : ULong
0210 
0211   alias AllocFunc = Void*, UInt, UInt -> Void*
0212   alias FreeFunc = (Void*, Void*) ->
0213 
0214   struct ZStream
0215     next_in : Bytef*
0216     avail_in : UInt
0217     next_out : Bytef*
0218     total_out : ULong
0219     msg : Char*
0220     state : Void*
0221     zalloc : AllocFunc
0222     zfree : FreeFunc
0223     opaque : Void*
0224     data_type : Int
0225     adler : Long
0226   end
0227 
0228   # error codes
0229   enum Error
0230     OK            =  0
0231     VERSION_ERROR = -6
0232   end
0233 
0234   enum Flush
0235     NO_FLUSH      = 0
0236     TREES         = 6
0237   end
0238 
0239   MAX_BITS      = 15
0240 
0241   fun deflateInit2 = deflateInit2_(stream : ZStream*, level : Int32, method : Int32,
0242                                    window_bits : Int32, mem_level : Int32, strategy : Int32,
0243                                    version : UInt8*, stream_size : Int32) : Error
0244   fun deflate(stream : ZStream*, flush : Flush) : Error
0245   fun deflateSetDictionary(stream : ZStream*, dictionary : UInt8*, len : UInt) : Int
0246 
0247   fun inflateInit2 = inflateInit2_(stream : ZStream*, window_bits : Int32, version : UInt8*, stream_size : Int32) : Error
0248   fun inflate(stream : ZStream*, flush : Flush) : Error
0249   fun inflateSetDictionary(stream : ZStream*, dictionary : UInt8*, len : UInt) : Error
0250 end